Prerequisites:
- Swift 6
- Xcode 16
To use Swift Testing, your target needs to be compiled using Swift 6 and you need to be using Xcode 16.
What is Swift Testing?
Swift Testing is the new open source testing framework for testing Swift code.
- It is cross platform. Which means it can be used on all Apple platforms as well as Linux and Windows
- Tests can now be ran in parallel on all physical devices for the first time ever
- Works seamlessly with Xcode cloud integration as you can view results in App Store Connect
- Tests runs are parallelized by default
If you need help getting your tests to run in parallel, Swift 6 can help with the new Data Race Safety. Or you can adopt it incrementally and use the .serialized tag.
@Suite
Suites allow you to group your tests together.
- Each test that runs, runs in it’s own Suite instance
- Can have subsuites
- Traits
@Test
- Freestanding
- Tags
CustomTestStringConvertible
Allows you to test all arguments rather than failing for the first argument that fails.
Traits
- Serialized
Note: that all of the following can be used on tests or suites
- Tags
- Enabled
- Disabled
- TimeLimit
- Bug
- Serialized
Confirmation
Can specify an expectedCount
Swift Testing for those familiar with XCTest
Swift Testing is the successor to the existing XCTest framework and the good news is that if you already know XCTest, learning Swift Testing will be a breeze.
How to migrate from XCTest to Swift Testing
Swift Testing and XCTest can exist in the same test target or test plan so you can begin migrating once your target is compiled with Swift 6 and you’re using Xcode 16.
- Swap import XCTest for import Testing
- Swap XCTAssert… for #expect
- Swap XCTUnwrap for #require
- Swap setup/teardown for init/deinit
- Swap expectations for confirmations
- Swap XCTFail for Issue.record
- Swap continueAfterFailure for #require
- Swap XCTSkip for @Suite(.disabled(“Message”))
- Swap XCTExpectFailure for withKnownIssue()
When to continue using XCTest
- When using UI automation APIs like XCUIApplication
- When using performance testing APIs like XCTMetric
- If your tests can only be written in Objective-C