Swift Testing
Swift Testing is the modern testing framework for Swift (Xcode 16+, Swift 6+). Prefer it for new unit tests. Keep XCTest where migration is still in progress, and use XCTest for UI automation, performance APIs, Objective-C exception tests, and common snapshot-test tooling.
Contents
- Basic Tests
@Test Traits- #expect and #require
@Suite and Test Organization- Execution Model
- XCTest Migration Boundaries
- Known Issues
- Additional Patterns
- Common Mistakes
- Test Attachments
- Exit Testing
- Version-Gated APIs
- Review Checklist
- References
Basic Tests
@Test Traits
#expect and #require
Rule: Use #require when subsequent assertions depend on the value. Use #expect for independent checks.
@Suite and Test Organization
See references/testing-patterns.md [blocked] for suite organization, confirmation patterns, known-issue handling, and execution-model details.
Execution Model
Swift Testing runs tests in parallel by default. Do not assume test order, shared suite instances, or exclusive access to mutable state unless you explicitly design for it.
Use .serialized when a test or suite must run one-at-a-time because it touches shared external state. It does not make unrelated tests outside that scope run serially.
Rules:
- Each test must set up its own state.
- Shared mutable globals are a bug unless protected or intentionally serialized.
@Suite(.serialized)is for exclusive execution, not for expressing logical ordering between tests.- If tests depend on sequence, combine them into one test or move the sequence into shared helper code.
XCTest Migration Boundaries
Swift Testing unit tests do not inherit from XCTestCase. Declare @Test on
free functions or methods on suite types such as struct, class, or actor;
use static or class methods when instance fixtures are unnecessary.
XCTest and Swift Testing can coexist during migration. Migrate one file or suite at a time, compare discovery/pass/fail/skip counts, and keep UI automation, performance benchmarks, and common snapshot flows on XCTest/XCUITest or snapshot tooling. Separate files or targets when that makes runner expectations clearer.
For Xcode 27-era mixed helpers, check the configured interoperability mode rather than claiming cross-framework APIs are forbidden. Older test plans inherit limited; new projects use complete; strict and none are also available. Prefer complete or strict during migration and use SWIFT_TESTING_XCTEST_INTEROP_MODE for SwiftPM when needed. See references/testing-advanced.md [blocked] for the mode matrix and toolchain gates.
Do not mechanically replace every XCTest assertion with #expect; preserve
required unwraps and unconditional failures with these migration defaults:
XCTAssert*->#expect(...)XCTUnwrapor any value required by later checks ->try #require(...)XCTFail("...")or manual unconditional issues ->Issue.record("...")- UI tests, performance benchmarks, and common snapshot-test flows stay on XCTest/XCUITest or snapshot tooling.
- Put
@availableon individual@Testfunctions, not on suite types or their containing types.
See references/testing-patterns.md [blocked] for migration examples and references/testing-advanced.md [blocked] for Swift/Xcode version gates.
Known Issues
Mark expected failures so they do not cause test failure:
If no known issues are recorded, Swift Testing records a distinct issue notifying you the problem may be resolved.
Additional Patterns
See references/testing-patterns.md [blocked] for parameterized tests, tags and suites, async testing, traits, and execution-model details.
Test Attachments
Attach diagnostic data to test results for debugging failures. See references/testing-patterns.md [blocked] for full examples.
For image attachments and their toolchain gate, use the canonical table in Version-Gated APIs.
Exit Testing
Test code that calls exit(), fatalError(), or preconditionFailure() on a
supported runtime. State the exact gate from Version-Gated APIs
when correcting exit-test code.
Version-Gated APIs
For advanced APIs, state the exact toolchain and runtime gate beside the correction. This is the canonical summary; references/testing-advanced.md [blocked] contains the detailed matrix and examples.
Common Mistakes
- Testing implementation, not behavior. Test what the code does, not how.
- No error path tests. If a function can throw, test the throw path.
- Flaky async tests or sleeps. Use
confirmation, clock injection, or concurrency primitives instead of sleeping. - Shared mutable state between tests. Each test sets up its own state via
init()in@Suite. - Missing accessibility identifiers in UI tests. XCUITest queries rely on them.
- Not testing cancellation. If code supports
Taskcancellation, verify it cancels cleanly. - Unclear migration boundaries. Follow XCTest Migration Boundaries instead of treating either framework as an all-or-nothing choice.
- Non-Sendable helpers shared across tests. Make shared helpers
Sendable; annotate MainActor-dependent test code with@MainActor. - Treating serialization as ordering. Tests run in parallel by default;
.serializedprotects exclusive state but does not make one test feed another.
Review Checklist
- All new tests use Swift Testing (
@Test,#expect), not XCTest assertions - Test names describe behavior (
fetchUserReturnsNilOnNetworkErrornottestFetchUser) - Error paths have dedicated tests
- Async tests use
confirmation(), notTask.sleep - Parameterized tests used for repetitive variations
- Tags applied for filtering (
.critical,.slow) - Mocks conform to protocols, not subclass concrete types
- No shared mutable state between tests
- Tests do not rely on declaration order or shared suite instances
-
.serializedused only for truly exclusive state, not to model workflow sequencing - Cancellation tested for cancellable async operations
References
- Testing patterns: references/testing-patterns.md [blocked]
- Advanced testing (warnings, cancellation, image attachments): references/testing-advanced.md [blocked]
