-
Notifications
You must be signed in to change notification settings - Fork 128
Add experimental SPI to cancel a running test. #1284
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This PR introduces `Test.cancel()` and `Test.Case.cancel()` which cancel the current test/suite and the current test case, respectively. For example: ```swift @test(arguments: [Food.burger, .fries, .iceCream]) func `Food truck is well-stocked`(_ food: Food) throws { if food == .iceCream && Season.current == .winter { try Test.Case.cancel("It's too cold for ice cream.") } // ... } ``` These functions work by cancelling the child task associated with the current test or test case, then throwing an error to end local execution early. Compare `XCTSkip()` which, in Swift, is just a thrown error that the XCTest harness special-cases, or `XCTSkip()` in Objective-C which actually throws an exception to force the caller to exit early.
Contributor
Author
|
@swift-ci test |
Contributor
Author
|
@swift-ci test |
Contributor
Author
|
@swift-ci test |
Contributor
Author
|
@swift-ci test |
Contributor
Author
|
@swift-ci test |
… the test (if the test's task is actually cancelled)
Contributor
Author
|
@swift-ci test |
Contributor
Author
|
@swift-ci test |
1 similar comment
Contributor
Author
|
@swift-ci test |
Contributor
Author
|
@swift-ci test |
Contributor
Author
|
@swift-ci test |
Contributor
Author
|
@swift-ci test |
Contributor
Author
|
@swift-ci test |
Contributor
Author
|
@swift-ci test |
Contributor
Author
|
@swift-ci test |
…aluation if the task was cancelled
Contributor
Author
|
@swift-ci test |
stmontgomery
approved these changes
Aug 27, 2025
Contributor
Author
|
@swift-ci test |
Contributor
Author
|
@swift-ci test |
Contributor
Author
|
@swift-ci test |
stmontgomery
approved these changes
Aug 27, 2025
grynspan
added a commit
that referenced
this pull request
Sep 2, 2025
This PR extends the special-casing of `SkipInfo` and `CancellationError` added in #1284 to also cover test case evaluation during test planning. If either error is thrown from `evaluateTestCases()`, we treat it as test cancellation the same way we do for trait evaluation.
grynspan
added a commit
that referenced
this pull request
Sep 2, 2025
This PR extends the special-casing of `SkipInfo` and `CancellationError` added in #1284 to also cover test case evaluation during test planning. If either error is thrown from `evaluateTestCases()`, we treat it as test cancellation the same way we do for trait evaluation.
2 tasks
grynspan
added a commit
that referenced
this pull request
Sep 2, 2025
This PR extends the special-casing of `SkipInfo` and `CancellationError` added in #1284 to also cover test case evaluation during test planning. If either error is thrown from `evaluateTestCases()`, we treat it as test cancellation the same way we do for trait evaluation. Example: ```swift func websites() async throws -> [Website] { guard let www = Web() else { try Test.cancel("The Web doesn't exist, is this 1992?") } return try await www.downloadEverything() } @test(arguments: try await websites()) func browseWeb(at website: Website) { ... } ``` ### Checklist: - [x] Code and documentation should follow the style of the [Style Guide](https://github.com/apple/swift-testing/blob/main/Documentation/StyleGuide.md). - [x] If public symbols are renamed or modified, DocC references should be updated.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
enhancement
New feature or request
issue-handling
Related to Issue handling within the testing library
public-api
Affects public API
traits
Issues and PRs related to the trait subsystem or built-in traits
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR introduces
Test.cancel()andTest.Case.cancel()which cancel the current test/suite and the current test case, respectively. For example:These functions work by cancelling the child task associated with the current test or test case, then throwing an error to end local execution early. Compare
XCTSkip()which, in Swift, is just a thrown error that the XCTest harness special-cases, orXCTSkip()in Objective-C which actually throws an exception to force the caller to exit early.Resolves #120.
Resolves #1289.
Resolves rdar://159150449.
Checklist: