12
12
Many projects want to migrate from XCTest to Swift Testing, and may be in an
13
13
intermediate state where test helpers written using XCTest API are called from
14
14
Swift 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.
19
19
20
20
## Motivation
21
21
@@ -44,19 +44,21 @@ class FooTests: XCTestCase {
44
44
}
45
45
```
46
46
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:
48
49
49
50
- You call XCTest API in a Swift Testing test, or call Swift Testing API in a
50
51
XCTest test,
51
52
- The API doesn't function as expected in some or all cases, and
52
53
- You get no notice at build time or runtime about the malfunction
53
54
54
55
For 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** .
56
57
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.
60
62
61
63
## Proposed solution
62
64
@@ -106,7 +108,7 @@ We also propose highlighting usage of above XCTest APIs in Swift Testing:
106
108
107
109
- ** Report [ runtime warning issues] [ ] ** for XCTest API usage in Swift Testing.
108
110
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.
110
112
111
113
- Opt-in ** strict interop mode** , where XCTest API usage will result in
112
114
` fatalError("Usage of XCTest API in a Swift Testing context is unsupported") ` .
@@ -123,7 +125,8 @@ Here are some concrete examples:
123
125
124
126
We propose supporting the following Swift Testing APIs in XCTest:
125
127
126
- - ` #expect ` and ` #require `
128
+ - [ ` #expect ` and ` #require ` ] [ Swift Testing expectations ]
129
+ - Includes [ ` #expect(throws:) ` ] [ testing for errors ]
127
130
- Includes [ exit testing] [ ]
128
131
- ` withKnownIssue ` : marking an XCTest issue in this way will generate a runtime
129
132
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
144
147
support interop of the new API with XCTest.
145
148
146
149
On 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
148
151
interoperability for traits under this proposal.
149
152
150
153
Here are some concrete examples:
@@ -174,21 +177,18 @@ Here are some concrete examples:
174
177
Configure the interoperability mode when running tests using the
175
178
` SWIFT_TESTING_XCTEST_INTEROP_MODE ` environment variable:
176
179
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 ` |
178
181
| ------------ | -------------------------------------------------------------------------- | ---------------------------------------------- |
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 ` |
180
183
| Permissive | XCTest API: ⚠️ Runtime Warning Issue. All Issues: ❌ Test Failure | ` permissive ` , or empty value, or invalid value |
181
184
| Strict | XCTest API: 💥 ` fatalError ` . Swift Testing API: ❌ Test Failure | ` strict ` |
182
185
183
- ### Phased Rollout
186
+ ## Source compatibility
184
187
185
188
When 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.
190
190
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
192
192
lead to situations where previously "passing" test code now starts showing
193
193
failures. We believe this should be a net positive if it can highlight actual
194
194
bugs you would have missed previously.
@@ -199,31 +199,31 @@ interoperability.
199
199
200
200
## Integration with supporting tools
201
201
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
-
207
202
- Swift packages: ` swift-tools-version ` declared in Package.swift will be used
208
203
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).
209
206
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.
211
209
212
210
- 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.
214
212
215
213
## Future directions
216
214
217
215
There's still more we can do to make it easier to migrate from XCTest to Swift
218
216
Testing:
219
217
218
+ - In a future release, we would consider making strict interop mode the default.
219
+
220
220
- Provide fixups at compile-time to replace usage of XCTest API with the
221
221
corresponding Swift Testing API, e.g. replace ` XCTAssert ` with ` #expect ` .
222
222
However, this would require introspection of the test body to look for XCTest
223
223
API usage, which would be challenging to do completely and find usages of this
224
224
API within helper methods.
225
225
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
227
227
interoperability with XCTest.
228
228
229
229
## Alternatives considered
@@ -263,16 +263,16 @@ that we want users to migrate to Swift Testing.
263
263
However, we are especially sensitive to use cases that depend upon the currently
264
264
lossy without interop APIs. With strict interop mode, the test process will
265
265
crash 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.
270
270
271
271
### Alternative methods to control interop mode
272
272
273
273
- ** 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.
276
276
277
277
However, interop is a runtime concept, and would be difficult or at least
278
278
non-idiomatic to modify with a build setting.
@@ -292,11 +292,13 @@ Thanks to Stuart Montgomery, Jonathan Grynspan, and Brian Croom for feedback on
292
292
the proposal.
293
293
294
294
[ 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
295
297
[ XCTest attachments ] : https://developer.apple.com/documentation/xctest/adding-attachments-to-tests-activities-and-issues
296
298
[ unconditional failure ] : https://developer.apple.com/documentation/xctest/unconditional-test-failures
297
299
[ runtime warning issues ] : https://github.com/swiftlang/swift-evolution/blob/main/proposals/testing/0013-issue-severity-warning.md
298
300
[ expected failures ] : https://developer.apple.com/documentation/xctest/expected-failures
299
301
[ issue handling traits ] : https://developer.apple.com/documentation/testing/issuehandlingtrait
300
302
[ 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
302
304
[ exit testing ] : https://developer.apple.com/documentation/testing/exit-testing
0 commit comments