Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upBranch:master
Clone or download
Launching GitHub Desktop...
If nothing happens,download GitHub Desktop and try again.
Launching GitHub Desktop...
If nothing happens,download GitHub Desktop and try again.
Launching Xcode...
If nothing happens,download Xcode and try again.
Launching Visual Studio...
If nothing happens,download the GitHub extension for Visual Studio and try again.
| Type | Name | Latest commit message | Commit time |
|---|---|---|---|
| Failed to load latest commit information. | |||
![]() | Sources/XCTest | Merge pull request#255from compnerd/foundation-over-core | |
![]() | Tests/Functional | ||
![]() | XCTest.xcodeproj | ||
![]() | XCTest.xcworkspace | ||
![]() | cmake/modules | [cmake] Skip regex when CMAKE_STATIC_LIBRARY_PREFIX is empty. | |
![]() | .gitignore | gitignore: add vim swap files to gitignore | |
![]() | .mailmap | Add mailmap for git history. | |
![]() | .pep8 | [Python] Add flake8 config | |
![]() | CMakeLists.txt | build: permit building without LLVM | |
![]() | CONTRIBUTING.md | Add legal notice for pull requests and reference to contribution guid… | |
![]() | Info.plist | Add iOS, tvOS, watchOS platforms support | |
![]() | LICENSE | ||
![]() | Package.swift | ||
![]() | README.md | ||
![]() | build_script.py | add a linker search path to account for libraries being moved to oldp… | |
README.md
XCTest
The XCTest library is designed to provide a common framework for writing unit tests in Swift, for Swift packages and applications.
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.
Using XCTest
Your tests are organized into a simple hierarchy. EachXCTestCase subclass has a set oftest methods, each of which should test one part of your code.
For general information about using XCTest, see:
Using XCTest with Swift Package Manager
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'sTests subdirectory, and you can build and run the full test suite in one step by runningswift test.
For more information about using XCTest with SwiftPM, see itsdocumentation.
Test Method Discovery
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.
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 XCTestwithout SwiftPM, orwith 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.
The recommended way to do this is to create a static property in each of yourXCTestCase subclasses. By convention, this property is namedallTests, and should contain all of the tests in the class. For example:
classTestNSURL :XCTestCase{staticvar allTests= {return [ ("test_bestNumber", test_bestNumber), ("test_URLStrings", test_URLStrings), ("test_fileURLWithPath", test_fileURLWithPath),// Other tests go here ] }()functest_bestNumber() {// Write your test here. Most of the XCTAssert functions you are familiar with are available.XCTAssertTrue(theBestNumber==42,"The number is wrong") }// Other tests go here}
After creating anallTests property in eachXCTestCase subclass, you must tell XCTest about those classes' tests.
If the project is a SwiftPM package which supports macOS, the easiest way to do this is to runswift test --generate-linuxmain from a macOS machine. This command generates files within the package'sTests 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 wheneverXCTestCase subclasses or test methods are added to or removed from your package's test suite.
If the project is a SwiftPM package but does not support macOS, you may edit the package's defaultLinuxMain.swift file manually to add allXCTestCase subclasses.
If the project is not a SwiftPM package, follow the steps in the next section to create an executable which calls theXCTMain function manually.
Standalone Command Line Usage
When used by itself, without SwiftPM, this version of XCTest does not use the externalxctest CLI test runner included with Xcode to run tests. Instead, you must create your own executable which linkslibXCTest.so, and in yourmain.swift, invoke theXCTMain function with an array of the tests from allXCTestCase subclasses that you wish to run, wrapped by thetestCase helper function. For example:
XCTMain([testCase(TestNSString.allTests),testCase(TestNSArray.allTests),testCase(TestNSDictionary.allTests),])
TheXCTMain function does not return, and will cause your test executable to exit with either0 for success or1 for failure. Certain command line arguments can be used to modify the test runner behavior:
- A particular test or test case can be selected to execute. For example:
$ ./FooTests Tests.FooTestCase/testFoo # Run a single test case$ ./FooTests Tests.FooTestCase # Run all the tests in FooTestCase- Tests can be listed, instead of executed.
$ ./FooTests --list-testsListing 4 tests in FooTests.xctest:Tests.FooTestCase/testFooTests.FooTestCase/testBarTests.BarTestCase/test123$ ./FooTests --dump-tests-json{"tests":[{"tests":[{"tests":[{"name":"testFoo"},{"name":"testBar"}],"name":"Tests.FooTestCase"},{"tests":[{"name":"test123"}],"name":"Tests.BarTestCase"}],"name":"Tests.xctest"}],"name":"All tests"}Contributing to XCTest
To contribute, you'll need to be able to build this project and and run its test suite. The easiest way to do so is via the Swift build script.
First, followthe instructions in the Swift README to build Swift from source. Confirm you're able to build the Swift project usingutils/build-script -R.
Once you are able to build the Swift project, build XCTest and run its tests:
$ cd swift-corelibs-xctest$ ../swift/utils/build-script --preset corelibs-xctestThis project is only guaranteed to build with the very latest commit on the Swift and swift-corelibs-foundationmaster branches. You may update to the latest commits using the Swiftutils/update-checkout script:
$ ../swift/utils/update-checkoutUsing Xcode
To browse files in this project using Xcode, useXCTest.xcworkspace. You may build the project using theSwiftXCTest scheme. Run theSwiftXCTestFunctionalTests scheme to run the tests.
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 providesXcode toolchains to download, as well asinstructions on how to use Xcode with those toolchains. Swift development moves fairly quickly, and so even a week-old toolchain may no longer work.
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-toolchainscript in the Swift repository.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.


