Skip to content

Add experimental SPI to cancel a running test.#1284

Merged
grynspan merged 25 commits into
mainfrom
jgrynspan/test-cancellation
Aug 27, 2025
Merged

Add experimental SPI to cancel a running test.#1284
grynspan merged 25 commits into
mainfrom
jgrynspan/test-cancellation

Conversation

@grynspan

@grynspan grynspan commented Aug 21, 2025

Copy link
Copy Markdown
Contributor

This PR introduces Test.cancel() and Test.Case.cancel() which cancel the current test/suite and the current test case, respectively. For example:

@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.

Resolves #120.
Resolves #1289.
Resolves rdar://159150449.

Checklist:

  • Code and documentation should follow the style of the Style Guide.
  • If public symbols are renamed or modified, DocC references should be updated.

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.
@grynspan grynspan added this to the Swift 6.x (main) milestone Aug 21, 2025
@grynspan grynspan self-assigned this Aug 21, 2025
@grynspan grynspan added the enhancement New feature or request label Aug 21, 2025
@grynspan grynspan added the public-api Affects public API label Aug 21, 2025
@grynspan grynspan added issue-handling Related to Issue handling within the testing library traits Issues and PRs related to the trait subsystem or built-in traits labels Aug 21, 2025
@grynspan

Copy link
Copy Markdown
Contributor Author

@swift-ci test

@grynspan

Copy link
Copy Markdown
Contributor Author

@swift-ci test

@grynspan

Copy link
Copy Markdown
Contributor Author

@swift-ci test

@grynspan

Copy link
Copy Markdown
Contributor Author

@swift-ci test

@grynspan

Copy link
Copy Markdown
Contributor Author

@swift-ci test

@grynspan

Copy link
Copy Markdown
Contributor Author

@swift-ci test

@grynspan

Copy link
Copy Markdown
Contributor Author

@swift-ci test

1 similar comment
@grynspan

Copy link
Copy Markdown
Contributor Author

@swift-ci test

@grynspan

Copy link
Copy Markdown
Contributor Author

@swift-ci test

@grynspan

Copy link
Copy Markdown
Contributor Author

@swift-ci test

@grynspan

Copy link
Copy Markdown
Contributor Author

@swift-ci test

@grynspan

Copy link
Copy Markdown
Contributor Author

@swift-ci test

@grynspan

Copy link
Copy Markdown
Contributor Author

@swift-ci test

@grynspan

Copy link
Copy Markdown
Contributor Author

@swift-ci test

@grynspan

Copy link
Copy Markdown
Contributor Author

@swift-ci test

Comment thread Sources/Testing/ABI/Encoded/ABI.EncodedAttachment.swift
Comment thread Sources/Testing/Events/Event.swift
Comment thread Sources/Testing/Running/Runner.swift
Comment thread Sources/Testing/Running/Runner.Plan.swift
@grynspan

Copy link
Copy Markdown
Contributor Author

@swift-ci test

@grynspan

Copy link
Copy Markdown
Contributor Author

@swift-ci test

Comment thread Sources/Testing/ABI/Encoded/ABI.EncodedEvent.swift
@stmontgomery stmontgomery self-requested a review August 27, 2025 17:45
@grynspan

Copy link
Copy Markdown
Contributor Author

@swift-ci test

@grynspan grynspan merged commit 64c8ce6 into main Aug 27, 2025
3 checks passed
@grynspan grynspan deleted the jgrynspan/test-cancellation branch August 27, 2025 19:14
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.
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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Task cancellation within a test is handled inconsistently XCSkip equivalents in swift-testing

2 participants