Skip to content

Commit 69667e5

Browse files
authored
Merge pull request #226 from stmontgomery/update-readme
Update XCTest's README file
2 parents 65d0eea + 99bbdba commit 69667e5

File tree

2 files changed

+80
-68
lines changed

2 files changed

+80
-68
lines changed

Documentation/Linux.md

-55
This file was deleted.

README.md

+80-13
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,92 @@
22

33
The XCTest library is designed to provide a common framework for writing unit tests in Swift, for Swift packages and applications.
44

5-
This version of XCTest uses the same API as the XCTest you are familiar with from Xcode. Our goal is to enable your project's tests to run on all Swift platforms without having to rewrite them.
5+
This version of XCTest implements the majority of unit testing APIs included in XCTest from Xcode 7 and later. Its goal is to enable your project's tests to run on all the platforms Swift supports without having to rewrite them.
66

7-
## Current Status and Project Goals
7+
## Using XCTest
88

9-
This project is in the very earliest stages of development. It is scheduled to be part of the Swift 3 release.
9+
Your tests are organized into a simple hierarchy. Each `XCTestCase` subclass has a set of `test` methods, each of which should test one part of your code.
1010

11-
Only the most basic functionality is currently present. This year, we have the following goals for the project:
11+
For general information about using XCTest, see:
1212

13-
* Finish implementing support for the most important non-UI testing APIs present in XCTest for Xcode.
14-
* Develop an effective solution to the problem of test discoverability without the Objective-C runtime.
15-
* Provide support for efforts to standardize test functionality across the Swift stack.
13+
* [Testing with Xcode](https://developer.apple.com/library/mac/documentation/DeveloperTools/Conceptual/testing_with_xcode/chapters/03-testing_basics.html)
14+
* [XCTest API documentation](https://developer.apple.com/documentation/xctest)
1615

17-
For more details, visit the `Documentation` directory.
16+
### Using XCTest with Swift Package Manager
1817

19-
## Using XCTest
18+
The Swift Package Manager integrates directly with XCTest to provide a streamlined experience for unit testing SwiftPM packages. If you are using XCTest within a SwiftPM package, unit test files are located within the package's `Tests` subdirectory, and you can build and run the full test suite in one step by running `swift test`.
2019

21-
Your tests are organized into a simple hierarchy. Each `XCTestCase` subclass has a set of `test` methods, each of which should test one part of your code.
20+
For more information about using XCTest with SwiftPM, see its [documentation](https://github.com/apple/swift-package-manager).
21+
22+
### Test Method Discovery
23+
24+
Unlike the version of XCTest included with Xcode, this version does not use the Objective-C runtime to automatically discover test methods because that runtime is not available on all platforms Swift supports. This means that in certain configurations, the full set of test methods must be explicitly provided to XCTest.
25+
26+
When using XCTest via SwiftPM on macOS, this is not necessary because SwiftPM uses the version of XCTest included with Xcode to run tests. But when using this version of XCTest _without_ SwiftPM, or _with_ SwiftPM on a platform other than macOS (including Linux), the full set of test methods cannot be discovered automatically, and your test target must tell XCTest about them explicitly.
27+
28+
The recommended way to do this is to create a static property in each of your `XCTestCase` subclasses. By convention, this property is named `allTests`, and should contain all of the tests in the class. For example:
29+
30+
```swift
31+
class TestNSURL : XCTestCase {
32+
static var allTests = {
33+
return [
34+
("test_bestNumber", test_bestNumber),
35+
("test_URLStrings", test_URLStrings),
36+
("test_fileURLWithPath", test_fileURLWithPath),
37+
// Other tests go here
38+
]
39+
}()
40+
41+
func test_bestNumber() {
42+
// Write your test here. Most of the XCTAssert functions you are familiar with are available.
43+
XCTAssertTrue(theBestNumber == 42, "The number is wrong")
44+
}
45+
46+
// Other tests go here
47+
}
48+
```
49+
50+
After creating an `allTests` property in each `XCTestCase` subclass, you must tell XCTest about those classes' tests.
51+
52+
If the project is a SwiftPM package which supports macOS, the easiest way to do this is to run `swift test --generate-linuxmain` from a macOS machine. This command generates files within the package's `Tests` subdirectory which contains the necessary source code for passing all test classes and methods to XCTest. These files should be committed to source control and re-generated whenever `XCTestCase` subclasses or test methods are added to or removed from your package's test suite.
53+
54+
If the project is a SwiftPM package but does not support macOS, you may edit the package's default `LinuxMain.swift` file manually to add all `XCTestCase` subclasses.
2255

23-
You can find all kinds of useful information on using XCTest in [Apple's documentation](https://developer.apple.com/library/mac/documentation/DeveloperTools/Conceptual/testing_with_xcode/chapters/03-testing_basics.html).
56+
If the project is not a SwiftPM package, follow the steps in the next section to create an executable which calls the `XCTMain` function manually.
57+
58+
### Standalone Command Line Usage
59+
60+
When used by itself, without SwiftPM, this version of XCTest does not use the external `xctest` CLI test runner included with Xcode to run tests. Instead, you must create your own executable which links `libXCTest.so`, and in your `main.swift`, invoke the `XCTMain` function with an array of the tests from all `XCTestCase` subclasses that you wish to run, wrapped by the `testCase` helper function. For example:
61+
62+
```swift
63+
XCTMain([
64+
testCase(TestNSString.allTests),
65+
testCase(TestNSArray.allTests),
66+
testCase(TestNSDictionary.allTests),
67+
])
68+
```
69+
70+
The `XCTMain` function does not return, and will cause your test executable to exit with either `0` for success or `1` for failure. Certain command line arguments can be used to modify the test runner behavior:
71+
72+
* A particular test or test case can be selected to execute. For example:
73+
74+
```
75+
$ ./FooTests Tests.FooTestCase/testFoo # Run a single test case
76+
$ ./FooTests Tests.FooTestCase # Run all the tests in FooTestCase
77+
```
78+
* Tests can be listed, instead of executed.
79+
80+
```
81+
$ ./FooTests --list-tests
82+
Listing 4 tests in FooTests.xctest:
83+
84+
Tests.FooTestCase/testFoo
85+
Tests.FooTestCase/testBar
86+
Tests.BarTestCase/test123
87+
88+
$ ./FooTests --dump-tests-json
89+
{"tests":[{"tests":[{"tests":[{"name":"testFoo"},{"name":"testBar"}],"name":"Tests.FooTestCase"},{"tests":[{"name":"test123"}],"name":"Tests.BarTestCase"}],"name":"Tests.xctest"}],"name":"All tests"}
90+
```
2491

2592
## Contributing to XCTest
2693

@@ -43,10 +110,10 @@ $ ../swift/utils/update-checkout
43110

44111
### Using Xcode
45112

46-
To browse files in this project using Xcode, use `XCTest.xcworkspace`. You may build the project using the "SwiftXCTest" scheme. Run the "SwiftXCTestFunctionalTests" scheme to run the tests.
113+
To browse files in this project using Xcode, use `XCTest.xcworkspace`. You may build the project using the `SwiftXCTest` scheme. Run the `SwiftXCTestFunctionalTests` scheme to run the tests.
47114

48115
However, in order to successfully build the project in Xcode, **you must use an Xcode toolchain with an extremely recent version of Swift**. The Swift website provides [Xcode toolchains to download](https://swift.org/download/#latest-development-snapshots), as well as [instructions on how to use Xcode with those toolchains](https://swift.org/download/#apple-platforms). Swift development moves fairly quickly, and so even a week-old toolchain may no longer work.
49116

50-
> If none of the toolchains available to download are recent enough to build XCTest, you may build your own toolchain by using [the `utils/build-toolchain` script in the Swift repository](https://github.com/apple/swift/blob/master/utils/build-toolchain).
117+
> If none of the toolchains available to download are recent enough to build XCTest, you may build your own toolchain by using the [`utils/build-toolchain` script](https://github.com/apple/swift/blob/master/utils/build-toolchain) in the Swift repository.
51118
>
52119
> Keep in mind that the build script invocation in "Contributing to XCTest" above will always work, regardless of which Swift toolchains you have installed. The Xcode workspace exists simply for the convenience of contributors. It is not necessary to successfully build this project in Xcode in order to contribute.

0 commit comments

Comments
 (0)