swift-testing
Expert 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 tests, integration tests, or snapshot tests, (5) migrating from XCTest to Swift Testing, (6) TDD, Arrange-Act-Assert, or F.I.R.S.T. principles, (7) parameterized tests or test organization.
git clone https://github.com/bocato/swift-testing-agent-skill
T=$(mktemp -d) && git clone --depth=1 https://github.com/bocato/swift-testing-agent-skill "$T" && mkdir -p ~/.claude/skills && cp -r "$T/swift-testing" ~/.claude/skills/bocato-swift-testing-agent-skill-swift-testing && rm -rf "$T"
swift-testing/SKILL.mdSwift Testing
Overview
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.
Agent Behavior Contract (Follow These Rules)
- Use Swift Testing framework (
,@Test
,#expect
,#require
) for all new tests, not XCTest.@Suite - Always structure tests with clear Arrange-Act-Assert phases.
- Follow F.I.R.S.T. principles: Fast, Isolated, Repeatable, Self-Validating, Timely.
- Use proper test double terminology per Martin Fowler's taxonomy (Dummy, Fake, Stub, Spy, SpyingStub, Mock).
- Place fixtures close to models with
, not in test targets.#if DEBUG - Place test doubles close to interfaces with
, not in test targets.#if DEBUG - Prefer state verification over behavior verification - simpler, less brittle tests.
- Use
for soft assertions (continue on failure) and#expect
for hard assertions (stop on failure).#require
Quick Decision Tree
When a developer needs testing guidance, follow this decision tree:
-
Starting fresh with Swift Testing?
- Read
for suites, tags, traitsreferences/test-organization.md - Read
for async test patternsreferences/async-testing.md
- Read
-
Need to create test data?
- Read
for fixture patterns and placementreferences/fixtures.md - Read
for mock/stub/spy patternsreferences/test-doubles.md
- Read
-
Testing multiple inputs?
- Read
for parameterized testingreferences/parameterized-tests.md
- Read
-
Testing module interactions?
- Read
for integration test patternsreferences/integration-testing.md
- Read
-
Testing UI for regressions?
- Read
for snapshot testing setupreferences/snapshot-testing.md
- Read
-
Testing data structures or state?
- Read
for text-based snapshot testingreferences/dump-snapshot-testing.md
- Read
-
Migrating from XCTest?
- Read
for migration guidereferences/migration-xctest.md
- Read
Triage-First Playbook (Common Errors -> Next Best Move)
- "XCTAssertEqual is unavailable" / need to modernize tests
- Use
for XCTest to Swift Testing migrationreferences/migration-xctest.md
- Use
- Need to test async code
- Use
for async patterns, confirmation, timeoutsreferences/async-testing.md
- Use
- Tests are slow or flaky
- Check F.I.R.S.T. principles, use proper mocking per
references/test-doubles.md
- Check F.I.R.S.T. principles, use proper mocking per
- Need deterministic test data
- Use
for fixture patterns with fixed datesreferences/fixtures.md
- Use
- Need to test multiple scenarios efficiently
- Use
for parameterized testingreferences/parameterized-tests.md
- Use
- Need to verify component interactions
- Use
for integration test patternsreferences/integration-testing.md
- Use
Core Syntax
Basic Test
import Testing @Test func basicTest() { #expect(1 + 1 == 2) }
Test with Description
@Test("Adding items increases cart count") func addItem() { let cart = Cart() cart.add(item) #expect(cart.count == 1) }
Async Test
@Test func asyncOperation() async throws { let result = try await service.fetch() #expect(result.isValid) }
Arrange-Act-Assert Pattern
Structure every test with clear phases:
@Test func calculateTotal() { // Given let cart = ShoppingCart() cart.add(Item(price: 10)) cart.add(Item(price: 20)) // When let total = cart.calculateTotal() // Then #expect(total == 30) }
Assertions
#expect - Soft Assertion
Continues test execution after failure:
@Test func multipleExpectations() { let user = User(name: "Alice", age: 30) #expect(user.name == "Alice") // If fails, test continues #expect(user.age == 30) // This still runs }
#require - Hard Assertion
Stops test execution on failure:
@Test func requireExample() throws { let user = try #require(fetchUser()) // Stops if nil #expect(user.name == "Alice") }
Error Testing
@Test func throwsError() { #expect(throws: ValidationError.self) { try validate(invalidInput) } } @Test func throwsSpecificError() { #expect(throws: ValidationError.emptyField) { try validate("") } }
F.I.R.S.T. Principles
| Principle | Description | Application |
|---|---|---|
| Fast | Tests execute in milliseconds | Mock expensive operations |
| Isolated | Tests don't depend on each other | Fresh instance per test |
| Repeatable | Same result every time | Mock dates, network, external deps |
| Self-Validating | Auto-report pass/fail | Use , never rely on |
| Timely | Write tests alongside code | Use parameterized tests for edge cases |
Test Double Quick Reference
Per Martin Fowler's definition:
| Type | Purpose | Verification |
|---|---|---|
| Dummy | Fill parameters, never used | N/A |
| Fake | Working implementation with shortcuts | State |
| Stub | Provides canned answers | State |
| Spy | Records calls for verification | State |
| SpyingStub | Stub + Spy combined (most common) | State |
| Mock | Pre-programmed expectations, self-verifies | Behavior |
Important: What Swift community calls "Mock" is usually a SpyingStub.
For detailed patterns, see
references/test-doubles.md.
Test Double Placement
Place test doubles close to the interface, not in test targets:
// In PersonalRecordsCore-Interface/Sources/... public protocol PersonalRecordsRepositoryProtocol: Sendable { func getAll() async throws -> [PersonalRecord] func save(_ record: PersonalRecord) async throws } #if DEBUG public final class PersonalRecordsRepositorySpyingStub: PersonalRecordsRepositoryProtocol { // Spy: Captured calls public private(set) var savedRecords: [PersonalRecord] = [] // Stub: Configurable responses public var recordsToReturn: [PersonalRecord] = [] public var errorToThrow: Error? public func getAll() async throws -> [PersonalRecord] { if let error = errorToThrow { throw error } return recordsToReturn } public func save(_ record: PersonalRecord) async throws { if let error = errorToThrow { throw error } savedRecords.append(record) } } #endif
Fixtures
Place fixtures close to the model:
// In Sources/Models/PersonalRecord.swift public struct PersonalRecord: Equatable, Sendable { public let id: UUID public let weight: Double // ... } #if DEBUG extension PersonalRecord { public static func fixture( id: UUID = UUID(), weight: Double = 100.0 // ... defaults for all properties ) -> PersonalRecord { PersonalRecord(id: id, weight: weight) } } #endif
For detailed patterns, see
references/fixtures.md.
Test Pyramid
+-------------+ | UI Tests | 5% - End-to-end flows | (E2E) | +-------------+ | Integration | 15% - Module interactions | Tests | +-------------+ | Unit | 80% - Individual components | Tests | +-------------+
Reference Files
Load these files as needed for specific topics:
- Suites, tags, traits, parallel executiontest-organization.md
- Testing multiple inputs efficientlyparameterized-tests.md
- Async patterns, confirmation, timeouts, cancellationasync-testing.md
- Complete XCTest to Swift Testing migration guidemigration-xctest.md
- Complete taxonomy with examples (Dummy, Fake, Stub, Spy, SpyingStub, Mock)test-doubles.md
- Fixture patterns, placement, and best practicesfixtures.md
- Module interaction testing patternsintegration-testing.md
- UI regression testing with SnapshotTesting librarysnapshot-testing.md
- Text-based snapshot testing for data structuresdump-snapshot-testing.md
Best Practices Summary
- Use Swift Testing for new tests - Modern syntax, better features
- Follow Arrange-Act-Assert - Clear test structure
- Apply F.I.R.S.T. principles - Fast, Isolated, Repeatable, Self-Validating, Timely
- Place fixtures near models - With
guards#if DEBUG - Place test doubles near interfaces - With
guards#if DEBUG - Prefer state verification - Simpler, less brittle than behavior verification
- Use parameterized tests - For testing multiple inputs efficiently
- Follow test pyramid - 80% unit, 15% integration, 5% UI
Verification Checklist (When You Write Tests)
- Tests follow Arrange-Act-Assert pattern
- Test names describe behavior, not implementation
- Fixtures use sensible defaults, not random values
- Test doubles are minimal (only stub what's needed)
- Async tests use proper patterns (async/await, confirmation)
- Tests are fast (mock expensive operations)
- Tests are isolated (no shared state)
- Tests are repeatable (no flaky date/time dependencies)