This walks you through Apple's modern Swift Testing framework with the `@Test`, `#expect`, and `#require` macros instead of XCTest. It's opinionated about test structure (Arrange-Act-Assert), test double terminology (following Martin Fowler's taxonomy, not the community's loose "mock" usage), and where to put fixtures and doubles (close to models and interfaces with `#if DEBUG`, not in test targets). The decision tree is genuinely helpful for routing you to the right reference, whether you need parameterized tests, snapshot testing, or async patterns. If you're migrating from XCTest or trying to write less brittle integration tests, this gives you practical patterns worth following.
npx -y skills add bocato/swift-testing-agent-skill --skill swift-testing --agent claude-codeInstalls into .claude/skills of the current project.
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.
@Test, #expect, #require, @Suite) for all new tests, not XCTest.#if DEBUG, not in test targets.#if DEBUG, not in test targets.#expect for soft assertions (continue on failure) and #require for hard assertions (stop on failure).When a developer needs testing guidance, follow this decision tree:
Starting fresh with Swift Testing?
references/test-organization.md for suites, tags, traitsreferences/async-testing.md for async test patternsNeed to create test data?
references/fixtures.md for fixture patterns and placementreferences/test-doubles.md for mock/stub/spy patternsTesting multiple inputs?
references/parameterized-tests.md for parameterized testingTesting module interactions?
references/integration-testing.md for integration test patternsTesting UI for regressions?
references/snapshot-testing.md for snapshot testing setupTesting data structures or state?
references/dump-snapshot-testing.md for text-based snapshot testingMigrating from XCTest?
references/migration-xctest.md for migration guidereferences/migration-xctest.md for XCTest to Swift Testing migrationreferences/async-testing.md for async patterns, confirmation, timeoutsreferences/test-doubles.mdreferences/fixtures.md for fixture patterns with fixed datesreferences/parameterized-tests.md for parameterized testingreferences/integration-testing.md for integration test patternsimport Testing
@Test func basicTest() {
#expect(1 + 1 == 2)
}
@Test("Adding items increases cart count")
func addItem() {
let cart = Cart()
cart.add(item)
#expect(cart.count == 1)
}
@Test func asyncOperation() async throws {
let result = try await service.fetch()
#expect(result.isValid)
}
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)
}
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
}
Stops test execution on failure:
@Test func requireExample() throws {
let user = try #require(fetchUser()) // Stops if nil
#expect(user.name == "Alice")
}
@Test func throwsError() {
#expect(throws: ValidationError.self) {
try validate(invalidInput)
}
}
@Test func throwsSpecificError() {
#expect(throws: ValidationError.emptyField) {
try validate("")
}
}
| 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 #expect, never rely on print() |
| Timely | Write tests alongside code | Use parameterized tests for edge cases |
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.
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
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.
+-------------+
| UI Tests | 5% - End-to-end flows
| (E2E) |
+-------------+
| Integration | 15% - Module interactions
| Tests |
+-------------+
| Unit | 80% - Individual components
| Tests |
+-------------+
Load these files as needed for specific topics:
test-organization.md - Suites, tags, traits, parallel executionparameterized-tests.md - Testing multiple inputs efficientlyasync-testing.md - Async patterns, confirmation, timeouts, cancellationmigration-xctest.md - Complete XCTest to Swift Testing migration guidetest-doubles.md - Complete taxonomy with examples (Dummy, Fake, Stub, Spy, SpyingStub, Mock)fixtures.md - Fixture patterns, placement, and best practicesintegration-testing.md - Module interaction testing patternssnapshot-testing.md - UI regression testing with SnapshotTesting librarydump-snapshot-testing.md - Text-based snapshot testing for data structures#if DEBUG guards#if DEBUG guardswshobson/agents
dbt-labs/dbt-agent-skills
github/awesome-copilot