$npx -y skills add johnrogers/claude-swift-engineering --skill swift-testingUse when writing tests with Swift Testing (@Test, #expect, #require), migrating from XCTest, implementing async tests, or parameterizing tests.
| 1 | # Swift Testing Framework |
| 2 | |
| 3 | Modern testing with Swift Testing framework. No XCTest. |
| 4 | |
| 5 | ## Overview |
| 6 | |
| 7 | Swift Testing replaces XCTest with a modern macro-based approach that's more concise, has better async support, and runs tests in parallel by default. The core principle: if you learned XCTest, unlearn it—Swift Testing works differently. |
| 8 | |
| 9 | ## References |
| 10 | |
| 11 | - [Apple Documentation](https://developer.apple.com/documentation/testing) |
| 12 | - [Migration Guide](https://steipete.me/posts/2025/migrating-700-tests-to-swift-testing) |
| 13 | |
| 14 | ## Core Concepts |
| 15 | |
| 16 | ### Assertions |
| 17 | |
| 18 | | Macro | Use Case | |
| 19 | |-------|----------| |
| 20 | | `#expect(expression)` | Soft check — continues on failure. Use for most assertions. | |
| 21 | | `#require(expression)` | Hard check — stops test on failure. Use for preconditions only. | |
| 22 | |
| 23 | ### Optional Unwrapping |
| 24 | |
| 25 | ```swift |
| 26 | let user = try #require(await fetchUser(id: "123")) |
| 27 | #expect(user.id == "123") |
| 28 | ``` |
| 29 | |
| 30 | ## Test Structure |
| 31 | |
| 32 | ```swift |
| 33 | import Testing |
| 34 | @testable import YourModule |
| 35 | |
| 36 | @Suite |
| 37 | struct FeatureTests { |
| 38 | let sut: FeatureType |
| 39 | |
| 40 | init() throws { |
| 41 | sut = FeatureType() |
| 42 | } |
| 43 | |
| 44 | @Test("Description of behavior") |
| 45 | func testBehavior() { |
| 46 | #expect(sut.someProperty == expected) |
| 47 | } |
| 48 | } |
| 49 | ``` |
| 50 | |
| 51 | ## Assertion Conversions |
| 52 | |
| 53 | | XCTest | Swift Testing | |
| 54 | |--------|---------------| |
| 55 | | `XCTAssert(expr)` | `#expect(expr)` | |
| 56 | | `XCTAssertEqual(a, b)` | `#expect(a == b)` | |
| 57 | | `XCTAssertNil(a)` | `#expect(a == nil)` | |
| 58 | | `XCTAssertNotNil(a)` | `#expect(a != nil)` | |
| 59 | | `try XCTUnwrap(a)` | `try #require(a)` | |
| 60 | | `XCTAssertThrowsError` | `#expect(throws: ErrorType.self) { }` | |
| 61 | | `XCTAssertNoThrow` | `#expect(throws: Never.self) { }` | |
| 62 | |
| 63 | ## Error Testing |
| 64 | |
| 65 | ```swift |
| 66 | #expect(throws: (any Error).self) { try riskyOperation() } |
| 67 | #expect(throws: NetworkError.self) { try fetch() } |
| 68 | #expect(throws: NetworkError.timeout) { try fetch() } |
| 69 | #expect(throws: Never.self) { try safeOperation() } |
| 70 | ``` |
| 71 | |
| 72 | ## Parameterized Tests |
| 73 | |
| 74 | ```swift |
| 75 | @Test("Validates inputs", arguments: zip( |
| 76 | ["a", "b", "c"], |
| 77 | [1, 2, 3] |
| 78 | )) |
| 79 | func testInputs(input: String, expected: Int) { |
| 80 | #expect(process(input) == expected) |
| 81 | } |
| 82 | ``` |
| 83 | |
| 84 | **Warning:** Multiple collections WITHOUT zip creates Cartesian product. |
| 85 | |
| 86 | ## Async Testing |
| 87 | |
| 88 | ```swift |
| 89 | @Test func testAsync() async throws { |
| 90 | let result = try await fetchData() |
| 91 | #expect(!result.isEmpty) |
| 92 | } |
| 93 | ``` |
| 94 | |
| 95 | ### Confirmations |
| 96 | |
| 97 | ```swift |
| 98 | @Test func testCallback() async { |
| 99 | await confirmation("callback received") { confirm in |
| 100 | let sut = SomeType { confirm() } |
| 101 | sut.triggerCallback() |
| 102 | } |
| 103 | } |
| 104 | ``` |
| 105 | |
| 106 | ## Tags |
| 107 | |
| 108 | ```swift |
| 109 | extension Tag { |
| 110 | @Tag static var fast: Self |
| 111 | @Tag static var networking: Self |
| 112 | } |
| 113 | |
| 114 | @Test(.tags(.fast, .networking)) |
| 115 | func testNetworkCall() { } |
| 116 | ``` |
| 117 | |
| 118 | ## Common Pitfalls |
| 119 | |
| 120 | 1. **Overusing `#require`** — Use `#expect` for most checks |
| 121 | 2. **Forgetting state isolation** — Each test gets a NEW instance |
| 122 | 3. **Accidental Cartesian product** — Always use `zip` for paired inputs |
| 123 | 4. **Not using `.serialized`** — Apply for thread-unsafe legacy tests |
| 124 | |
| 125 | ## Common Mistakes |
| 126 | |
| 127 | 1. **Overusing `#require`** — `#require` is for preconditions only. Using it for normal assertions means the test stops at first failure instead of reporting all failures. Use `#expect` for assertions, `#require` only when subsequent assertions depend on the value. |
| 128 | |
| 129 | 2. **Cartesian product bugs** — `@Test(arguments: [a, b], [c, d])` creates 4 combinations, not 2. Always use `zip` to pair arguments correctly: `arguments: zip([a, b], [c, d])`. |
| 130 | |
| 131 | 3. **Forgetting state isolation** — Swift Testing creates a new test instance per test method. BUT shared state between tests (static variables, singletons) still leak. Use dependency injection or clean up singletons between tests. |
| 132 | |
| 133 | 4. **Parallel test conflicts** — Swift Testing runs tests in parallel by default. Tests touching shared files, databases, or singletons will interfere. Use `.serialized` or isolation strategies. |
| 134 | |
| 135 | 5. **Not using `async` naturally** — Wrapping async operations in `Task { }` defeats the purpose. Use `async/await` directly in test function signature: `@Test func testAsync() async throws { }`. |
| 136 | |
| 137 | 6. **Confirmation misuse** — `confirmation` is for verifying callbacks were called. Using it for assertions is wrong. Use `#expect` for assertions, `confirmation` for callback counts. |