$npx -y skills add bocato/swift-testing-agent-skill --skill swift-testingExpert guidance on Swift Testing best practices, patterns, and implementation. Use when developers mention: (1) Swift Testing, @Test, #expect, #require, or @Suite, (2) "use Swift Testing" or "modern testing patterns", (3) test doubles, mocks, stubs, spies, or fixtures, (4) unit t
| 1 | # Swift Testing |
| 2 | |
| 3 | ## Overview |
| 4 | |
| 5 | This skill provides expert guidance on Swift Testing, covering the modern Swift Testing framework, test doubles (mocks, stubs, spies), fixtures, integration testing, snapshot testing, and migration from XCTest. Use this skill to help developers write reliable, maintainable tests following F.I.R.S.T. principles and Arrange-Act-Assert patterns. |
| 6 | |
| 7 | ## Agent Behavior Contract (Follow These Rules) |
| 8 | |
| 9 | 1. Use Swift Testing framework (`@Test`, `#expect`, `#require`, `@Suite`) for all new tests, not XCTest. |
| 10 | 2. Always structure tests with clear Arrange-Act-Assert phases. |
| 11 | 3. Follow F.I.R.S.T. principles: Fast, Isolated, Repeatable, Self-Validating, Timely. |
| 12 | 4. Use proper test double terminology per Martin Fowler's taxonomy (Dummy, Fake, Stub, Spy, SpyingStub, Mock). |
| 13 | 5. Place fixtures close to models with `#if DEBUG`, not in test targets. |
| 14 | 6. Place test doubles close to interfaces with `#if DEBUG`, not in test targets. |
| 15 | 7. Prefer state verification over behavior verification - simpler, less brittle tests. |
| 16 | 8. Use `#expect` for soft assertions (continue on failure) and `#require` for hard assertions (stop on failure). |
| 17 | |
| 18 | ## Quick Decision Tree |
| 19 | |
| 20 | When a developer needs testing guidance, follow this decision tree: |
| 21 | |
| 22 | 1. **Starting fresh with Swift Testing?** |
| 23 | - Read `references/test-organization.md` for suites, tags, traits |
| 24 | - Read `references/async-testing.md` for async test patterns |
| 25 | |
| 26 | 2. **Need to create test data?** |
| 27 | - Read `references/fixtures.md` for fixture patterns and placement |
| 28 | - Read `references/test-doubles.md` for mock/stub/spy patterns |
| 29 | |
| 30 | 3. **Testing multiple inputs?** |
| 31 | - Read `references/parameterized-tests.md` for parameterized testing |
| 32 | |
| 33 | 4. **Testing module interactions?** |
| 34 | - Read `references/integration-testing.md` for integration test patterns |
| 35 | |
| 36 | 5. **Testing UI for regressions?** |
| 37 | - Read `references/snapshot-testing.md` for snapshot testing setup |
| 38 | |
| 39 | 6. **Testing data structures or state?** |
| 40 | - Read `references/dump-snapshot-testing.md` for text-based snapshot testing |
| 41 | |
| 42 | 7. **Migrating from XCTest?** |
| 43 | - Read `references/migration-xctest.md` for migration guide |
| 44 | |
| 45 | ## Triage-First Playbook (Common Errors -> Next Best Move) |
| 46 | |
| 47 | - "XCTAssertEqual is unavailable" / need to modernize tests |
| 48 | - Use `references/migration-xctest.md` for XCTest to Swift Testing migration |
| 49 | - Need to test async code |
| 50 | - Use `references/async-testing.md` for async patterns, confirmation, timeouts |
| 51 | - Tests are slow or flaky |
| 52 | - Check F.I.R.S.T. principles, use proper mocking per `references/test-doubles.md` |
| 53 | - Need deterministic test data |
| 54 | - Use `references/fixtures.md` for fixture patterns with fixed dates |
| 55 | - Need to test multiple scenarios efficiently |
| 56 | - Use `references/parameterized-tests.md` for parameterized testing |
| 57 | - Need to verify component interactions |
| 58 | - Use `references/integration-testing.md` for integration test patterns |
| 59 | |
| 60 | ## Core Syntax |
| 61 | |
| 62 | ### Basic Test |
| 63 | |
| 64 | ```swift |
| 65 | import Testing |
| 66 | |
| 67 | @Test func basicTest() { |
| 68 | #expect(1 + 1 == 2) |
| 69 | } |
| 70 | ``` |
| 71 | |
| 72 | ### Test with Description |
| 73 | |
| 74 | ```swift |
| 75 | @Test("Adding items increases cart count") |
| 76 | func addItem() { |
| 77 | let cart = Cart() |
| 78 | cart.add(item) |
| 79 | #expect(cart.count == 1) |
| 80 | } |
| 81 | ``` |
| 82 | |
| 83 | ### Async Test |
| 84 | |
| 85 | ```swift |
| 86 | @Test func asyncOperation() async throws { |
| 87 | let result = try await service.fetch() |
| 88 | #expect(result.isValid) |
| 89 | } |
| 90 | ``` |
| 91 | |
| 92 | ## Arrange-Act-Assert Pattern |
| 93 | |
| 94 | Structure every test with clear phases: |
| 95 | |
| 96 | ```swift |
| 97 | @Test func calculateTotal() { |
| 98 | // Given |
| 99 | let cart = ShoppingCart() |
| 100 | cart.add(Item(price: 10)) |
| 101 | cart.add(Item(price: 20)) |
| 102 | |
| 103 | // When |
| 104 | let total = cart.calculateTotal() |
| 105 | |
| 106 | // Then |
| 107 | #expect(total == 30) |
| 108 | } |
| 109 | ``` |
| 110 | |
| 111 | ## Assertions |
| 112 | |
| 113 | ### #expect - Soft Assertion |
| 114 | |
| 115 | Continues test execution after failure: |
| 116 | |
| 117 | ```swift |
| 118 | @Test func multipleExpectations() { |
| 119 | let user = User(name: "Alice", age: 30) |
| 120 | #expect(user.name == "Alice") // If fails, test continues |
| 121 | #expect(user.age == 30) // This still runs |
| 122 | } |
| 123 | ``` |
| 124 | |
| 125 | ### #require - Hard Assertion |
| 126 | |
| 127 | Stops test execution on failure: |
| 128 | |
| 129 | ```swift |
| 130 | @Test func requireExample() throws { |
| 131 | let user = try #require(fetchUser()) // Stops if nil |
| 132 | #expect(user.name == "Alice") |
| 133 | } |
| 134 | ``` |
| 135 | |
| 136 | ### Error Testing |
| 137 | |
| 138 | ```swift |
| 139 | @Test func throwsError() { |
| 140 | #expect(throws: ValidationError.self) { |
| 141 | try validate(invalidInput) |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | @Test func throwsSpecificError() { |
| 146 | #expect(throws: ValidationError.emptyField) { |
| 147 | try validate("") |
| 148 | } |
| 149 | } |
| 150 | ``` |
| 151 | |
| 152 | ## F.I.R.S.T. Principles |
| 153 | |
| 154 | | Principle | Description | Application | |
| 155 | |-----------|-------------|- |