1212Many projects want to migrate from XCTest to Swift Testing, and may be in an
1313intermediate state where test helpers written using XCTest API are called from
1414Swift Testing. Today, the Swift Testing and XCTest libraries stand mostly
15- independently, which means an ` XCTAssert ` failure in a Swift Testing test is
16- silently ignored. To address this, we formally declare a set of interoperability
17- principles and propose changes to the handling of specific APIs that will enable
18- users to migrate with confidence.
15+ independently, which means an ` XCTAssert ` failure in a Swift Testing test or an
16+ ` #expect ` failure in an XCTest test is silently ignored. To address this, we
17+ formally declare a set of interoperability principles and propose changes to the
18+ handling of specific APIs that will enable users to migrate with confidence.
1919
2020## Motivation
2121
@@ -44,19 +44,21 @@ class FooTests: XCTestCase {
4444}
4545```
4646
47- Generally, we get into trouble today when ALL the following conditions are met:
47+ Generally, you encounter the above limitation with testing APIs when _ all_ the
48+ following conditions are met:
4849
4950- You call XCTest API in a Swift Testing test, or call Swift Testing API in a
5051 XCTest test,
5152- The API doesn't function as expected in some or all cases, and
5253- You get no notice at build time or runtime about the malfunction
5354
5455For the remainder of this proposal, we’ll describe test APIs which exhibit this
55- problem as ** lossy without interop** .
56+ limitation as ** lossy without interop** .
5657
57- This problem risks regressing test coverage for projects which migrate to Swift
58- Testing. Furthermore, projects that have switched completely to Swift Testing
59- may want to go and ensure they don't inadvertently add XCTest API.
58+ You could regress test coverage if you migrate to Swift Testing without
59+ replacing usage of lossy without interop test APIs. Furthermore, you may want to
60+ ensure you don't inadvertently introduce new XCTest API after completing your
61+ Swift Testing migration.
6062
6163## Proposed solution
6264
@@ -106,7 +108,7 @@ We also propose highlighting usage of above XCTest APIs in Swift Testing:
106108
107109- ** Report [ runtime warning issues] [ ] ** for XCTest API usage in Swift Testing.
108110 This ** applies to both assertion failures _ and successes_ ** ! This notifies you
109- about opportunities to modernise even if your tests currently pass.
111+ about opportunities to modernize even if your tests currently pass.
110112
111113- Opt-in ** strict interop mode** , where XCTest API usage will result in
112114 ` fatalError("Usage of XCTest API in a Swift Testing context is unsupported") ` .
@@ -123,7 +125,8 @@ Here are some concrete examples:
123125
124126We propose supporting the following Swift Testing APIs in XCTest:
125127
126- - ` #expect ` and ` #require `
128+ - [ ` #expect ` and ` #require ` ] [ Swift Testing expectations ]
129+ - Includes [ ` #expect(throws:) ` ] [ testing for errors ]
127130 - Includes [ exit testing] [ ]
128131- ` withKnownIssue ` : marking an XCTest issue in this way will generate a runtime
129132 warning issue. In strict interop mode, this becomes a ` fatalError ` .
@@ -144,7 +147,7 @@ is analogous to `XCTSkip`. If that pitch were accepted, this proposal would
144147support interop of the new API with XCTest.
145148
146149On the other hand, [ traits] [ ] are a powerful Swift Testing feature which is not
147- related to any functionality in XCTest. Therefore, there would be
150+ related to any functionality in XCTest. Therefore, there would not be
148151interoperability for traits under this proposal.
149152
150153Here are some concrete examples:
@@ -174,21 +177,18 @@ Here are some concrete examples:
174177Configure the interoperability mode when running tests using the
175178` SWIFT_TESTING_XCTEST_INTEROP_MODE ` environment variable:
176179
177- | Interop Mode | Issue behaviour across framework boundary | ` SWIFT_TESTING_XCTEST_INTEROP_MODE ` |
180+ | Interop Mode | Issue behavior across framework boundary | ` SWIFT_TESTING_XCTEST_INTEROP_MODE ` |
178181| ------------ | -------------------------------------------------------------------------- | ---------------------------------------------- |
179- | Advisory | XCTest API: ⚠️ Runtime Warning Issue. All Issues: ⚠️ Runtime Warning Issue | ` warning-only ` |
182+ | Advisory | XCTest API: ⚠️ Runtime Warning Issue. All Issues: ⚠️ Runtime Warning Issue | ` advisory ` |
180183| Permissive | XCTest API: ⚠️ Runtime Warning Issue. All Issues: ❌ Test Failure | ` permissive ` , or empty value, or invalid value |
181184| Strict | XCTest API: 💥 ` fatalError ` . Swift Testing API: ❌ Test Failure | ` strict ` |
182185
183- ### Phased Rollout
186+ ## Source compatibility
184187
185188When interoperability is first available, "permissive" will be the default
186- interop mode enabled for new projects. In a future release, "strict" will become
187- the default interop mode.
188-
189- ## Source compatibility
189+ interop mode enabled for new projects.
190190
191- As the main goal of interoperability is to change behaviour , this proposal will
191+ As the main goal of interoperability is to change behavior , this proposal will
192192lead to situations where previously "passing" test code now starts showing
193193failures. We believe this should be a net positive if it can highlight actual
194194bugs you would have missed previously.
@@ -199,31 +199,31 @@ interoperability.
199199
200200## Integration with supporting tools
201201
202- Interoperability will be first available in future toolchain version,
203- hypothetically named ` 6.X ` , where permissive interop mode will be enabled for
204- projects. After that, a ` 7.Y ` release would make strict interop mode the
205- default.
206-
207202- Swift packages: ` swift-tools-version ` declared in Package.swift will be used
208203 to determine interop mode, regardless of the toolchain used to run tests.
204+ Specifically, it will use the default interop mode associated with that
205+ toolchain version ("permissive" for the initial release version).
209206
210- - Otherwise, installed toolchain version will be used to determine interop mode.
207+ - Otherwise, the default interop mode associated with the installed toolchain
208+ version will be used to determine interop mode.
211209
212210- Any project can use ` SWIFT_TESTING_XCTEST_INTEROP_MODE ` to override interop
213- mode at runtime, provided they are on toolchain version ` 6.X ` or newer
211+ mode at runtime.
214212
215213## Future directions
216214
217215There's still more we can do to make it easier to migrate from XCTest to Swift
218216Testing:
219217
218+ - In a future release, we would consider making strict interop mode the default.
219+
220220- Provide fixups at compile-time to replace usage of XCTest API with the
221221 corresponding Swift Testing API, e.g. replace ` XCTAssert ` with ` #expect ` .
222222 However, this would require introspection of the test body to look for XCTest
223223 API usage, which would be challenging to do completely and find usages of this
224224 API within helper methods.
225225
226- - After new API is added to Swift Testing in future, will need to evaluate for
226+ - When new API is added to Swift Testing, we will need to evaluate it for
227227 interoperability with XCTest.
228228
229229## Alternatives considered
@@ -263,16 +263,16 @@ that we want users to migrate to Swift Testing.
263263However, we are especially sensitive to use cases that depend upon the currently
264264lossy without interop APIs. With strict interop mode, the test process will
265265crash on the first instance of XCTest API usage in Swift Testing, completely
266- halting testing. In this same scenario, the default permissive interop mode
267- would record a runtime warning issue and continue the remaining test, which we
268- believe strikes a better balance between notifying users yet not being totally
269- disruptive to the testing flow.
266+ halting testing. In this same scenario, the proposed default permissive interop
267+ mode would record a runtime warning issue and continue the remaining test, which
268+ we believe strikes a better balance between notifying users yet not being
269+ totally disruptive to the testing flow.
270270
271271### Alternative methods to control interop mode
272272
273273- ** Build setting:** e.g. a new ` SwiftSetting ` that can be included in
274- Package.swift or an Xcode project . A project could then configure their test
275- targets to have a non-default interop mode.
274+ Package.swift. A project could then configure their test targets to have a
275+ non-default interop mode.
276276
277277 However, interop is a runtime concept, and would be difficult or at least
278278 non-idiomatic to modify with a build setting.
@@ -292,11 +292,13 @@ Thanks to Stuart Montgomery, Jonathan Grynspan, and Brian Croom for feedback on
292292the proposal.
293293
294294[ XCTest assertions ] : https://developer.apple.com/documentation/xctest/equality-and-inequality-assertions
295+ [ Swift Testing expectations ] : https://developer.apple.com/documentation/testing/expectations
296+ [ Testing for errors ] : https://developer.apple.com/documentation/testing/testing-for-errors-in-swift-code
295297[ XCTest attachments ] : https://developer.apple.com/documentation/xctest/adding-attachments-to-tests-activities-and-issues
296298[ unconditional failure ] : https://developer.apple.com/documentation/xctest/unconditional-test-failures
297299[ runtime warning issues ] : https://github.com/swiftlang/swift-evolution/blob/main/proposals/testing/0013-issue-severity-warning.md
298300[ expected failures ] : https://developer.apple.com/documentation/xctest/expected-failures
299301[ issue handling traits ] : https://developer.apple.com/documentation/testing/issuehandlingtrait
300302[ test cancellation ] : https://forums.swift.org/t/pitch-test-cancellation/81847
301- [ traits ] : https://swiftpackageindex. com/swiftlang/swift-testing/main /documentation/testing/traits
303+ [ traits ] : https://developer.apple. com/documentation/testing/traits
302304[ exit testing ] : https://developer.apple.com/documentation/testing/exit-testing
0 commit comments