Skip to content

Conversation

@grynspan
Copy link
Contributor

This PR ensures that we diagnose if @Suite or @Test is applied to a class (or method on such class) that subclasses XCTest.XCTest or XCTestSuite, not just XCTestCase.

This change also suppresses the emitted call to the helper function __invokeXCTest[Case]Method() if the suite type is known not to subclass XCTest.XCTest (e.g. because it's a struct.) This slightly improves the performance of non-class suite types (although probably not measurably so).

Finally, this change improves the emitted diagnostic when @Test is used on a method in an XCTest.XCTest subclass such that it explicitly states the reason why the attribute can't be used:

🛑 Attribute 'Test' cannot be applied to a function within class 'C' because it is a subclass of 'XCTest', 'XCTestCase', or 'XCTestSuite'

Checklist:

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

…ite`.

This PR ensures that we diagnose if `@Suite` or `@Test` is applied to a class (or method on such class) that subclasses [`XCTest.XCTest`](https://developer.apple.com/documentation/xctest/xctest) or [`XCTestSuite`](https://developer.apple.com/documentation/xctest/xctestsuite), not just [`XCTestCase`](https://developer.apple.com/documentation/xctest/xctestcase).

This change also suppresses the emitted call to the helper function `__invokeXCTest[Case]Method()` if the suite type is _known_ not to subclass `XCTest.XCTest` (e.g. because it's a `struct`.) This slightly improves the performance of non-class suite types (although probably not measurably so).

Finally, this change improves the emitted diagnostic when `@Test` is used on a method in an `XCTest.XCTest` subclass such that it explicitly states the reason why the attribute can't be used:

> 🛑 Attribute 'Test' cannot be applied to a function within class 'C' because it is a subclass of 'XCTest', 'XCTestCase', or 'XCTestSuite'
@grynspan grynspan added this to the Swift 6.3.0 milestone Dec 21, 2025
@grynspan grynspan self-assigned this Dec 21, 2025
@grynspan grynspan added performance 🏎️ Performance issues macros 🔭 Related to Swift macros such as @Test or #expect interoperability ↔️ Work related to Swift Testing and XCTest interoperability labels Dec 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

interoperability ↔️ Work related to Swift Testing and XCTest interoperability macros 🔭 Related to Swift macros such as @Test or #expect performance 🏎️ Performance issues

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants