Hello Swift Testers! I’d like your feedback on a proposal for targeted interoperability between Swift Testing and XCTest.
Many developers, perhaps including you, are eager to migrate tests from XCTest to Swift Testing. In reality, your project might not be able to modernize all XCTest test code in a single pass. Maybe your project is simply too large to update all the test code at once. Or, you might rely on a feature available in XCTest but not yet implemented in Swift Testing (please file an issue if so!). As a result, your codebase can have elements of both testing frameworks present.
If any of this is sounding familiar, that's because "Enable incremental adoption" is actually an important part of the Swift Testing vision! We encourage you to develop "footholds" for Swift Testing where possible and continue to adopt at your own pace.
Unfortunately, mixing an API call from one framework with a test from the other framework may not work as expected. For example, if you take an existing test helper function written for XCTest and call it in a Swift Testing test, it won't report the assertion failure:
func assertUnique(_ elements: [Int]) {
XCTAssertEqual(Set(elements).count, elements.count, "\(elements) has non unique elements")
}
// XCTest
class FooTests: XCTestCase {
func testDups() {
assertUnique([1, 2, 1]) // Fails as expected
}
}
// Swift Testing
@Test func `Duplicate elements`() {
assertUnique([1, 2, 1]) // Passes? Oh no!
}
Proposal
It can understandably be quite challenging to migrate if you don't know if your tests will work the same afterwards! We think supporting targeted interoperability between the Swift Testing and XCTest frameworks will make it easier to modernise your projects for Swift Testing. If you’re already all-in on Swift Testing, you can opt-in to strict interop mode to ensure you never inadvertently introduce XCTest API in the future!
| When running a Swift Testing test... | Current | Proposed | Proposed (strict) |
|---|---|---|---|
| XCTAssert failure is a... | fatalError identifying unsupported interop |
||
| XCTAssert success is a... | No-op | fatalError |
|
throw XCTSkip is a... |
Check out the full proposal for more details, including:
-
Specific APIs receiving interoperability support
-
Configurable interop modes
-
Phased rollout strategy
Please feel free to share any questions or concerns that come to mind! Here are a few questions to get you started:
-
Have you run into issues where you missed a test failure due to usage of an XCTAssert in Swift Testing?
-
Which interoperability features would help you the most in migrating to Swift Testing?
-
What other tools or features would help you adopt Swift Testing in your project?