agentleFS
fal3fal3/claude-skills-collection/skills/swift-unit-testing-skill/SKILL.md

Swift Unit Testing Skill

Skill18 starsChanged 57 days agoMITView on GitHub

Design, write, migrate, and review Swift unit and integration tests using Swift Testing by default, with XCTest retained for UI automation, performance metrics, Objective-C interoperability, and legacy suites. Use for @Test, #expect, #require, parameterized tests, async tests, dependency injection, mocks/fakes, test plans, XCTest, XCUITest, TDD, flaky tests, and test architecture. Do not use UI-test polling or XCTest subclasses for ordinary new Swift unit tests, and do not introduce third-party snapshot frameworks unless the user requests one.

The file

Show its rules and text

We split this skill into the rules it gives an agent

18 rules. About 0 are common, 10 are distinctive, and 8 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈10

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 8

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Treat APIs first introduced after the installed SDK as future-cycle features and gate them explicitly.

    line 16
  2. Distinctive:

    Do not replace a unit test with a UI test.

    line 28
  3. Distinctive:

    Write a failing test, implement the smallest behavior, then refactor.

    line 35
  4. Distinctive:

    Run the narrow test, its containing suite, and then the affected test plan.

    line 36
  5. Distinctive:

    Check parallel safety: avoid mutable globals, shared files, fixed ports, real sleeps, and order dependence.

    line 37
  6. Very specific:

    Use #expect for nonfatal checks and try #require when later assertions need a value.

    line 42
  7. Distinctive:

    Prefer parameterized tests to copied methods:

    line 56
  8. Very specific:

    Use #expect(throws:) for failures and confirmation for callback counts.

    line 65
  9. Very specific:

    Read examples/example_basic_test.swift for standalone Swift Testing structure and parameterization.

    line 67
  10. Distinctive:

    Always complete every callback path exactly once.

    line 80
  11. Very specific:

    Wait for state transitions with waitForExistence(timeout:) or an NSPredicate expectation.

    line 87
  12. Very specific:

    Use XCTest metrics and baselines instead of a one-shot Date threshold.

    line 110
  13. Very specific:

    Prefer actors or immutable Sendable values in doubles used across tasks.

    line 123
  14. Distinctive:

    Verify cancellation and cleanup, not only successful completion.

    line 125
  15. Very specific:

    Do not silence Sendable diagnostics with @unchecked Sendable unless the type has a documented synchronization invariant.

    line 126
  16. Distinctive:

    Every asynchronous path is awaited and bounded by the test runner.

    line 132
  17. Distinctive:

    No real network, wall-clock sleep, shared account, or random order controls the result.

    line 133
  18. Very specific:

    When adapting an example to an app, move the sample system-under-test declaration into the production target and replace it in the test …

    line 146

≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Swift Unit Testing

Build deterministic tests around observable behavior. Prefer Swift Testing for new Swift unit and integration tests; choose XCTest only where it still provides the required runner or API.

Compatibility baseline

  • Swift Testing requires Swift 6 / Xcode 16 or newer. It supports Apple platforms, Linux, and Windows.
  • XCTest remains appropriate for XCUIApplication, XCTMetric, Objective-C tests, and incremental maintenance of existing XCTest suites.
  • Swift Testing and XCTest can coexist in one test target. Do not mix @Test methods into an XCTestCase subclass.
  • State the app's actual deployment targets and CI Xcode version before recommending availability-sensitive traits or APIs.
  • Treat APIs first introduced after the installed SDK as future-cycle features and gate them explicitly.

Select the test layer

GoalDefault
Pure Swift unit or integration behaviorSwift Testing
Async/throws, parameterized cases, traits, tagsSwift Testing
UIKit/SwiftUI end-to-end interactionXCTest UI testing
Runtime, CPU, memory, launch, or signpost metricsXCTest performance APIs
Objective-C test code or legacy suiteXCTest

Do not replace a unit test with a UI test. Keep network, database, clock, randomness, and notification boundaries injectable so unit tests remain fast and deterministic.

Workflow

  1. Identify the behavior and public boundary under test.
  2. List failure cases, cancellation behavior, and state transitions before writing assertions.
  3. Inject dependencies through small capability protocols or closures. Prefer a semantic transport protocol over subclassing framework classes.
  4. Write a failing test, implement the smallest behavior, then refactor.
  5. Run the narrow test, its containing suite, and then the affected test plan.
  6. Check parallel safety: avoid mutable globals, shared files, fixed ports, real sleeps, and order dependence.
  7. Confirm the test fails when the production behavior is deliberately broken.

Swift Testing defaults

Use #expect for nonfatal checks and try #require when later assertions need a value. Test asynchronous functions directly:

import Testing
@testable import MyApp

@Test("The profile includes the requested user")
func profileLookup() async throws {
    let profile = try await ProfileStore.preview.profile(id: 42)
    #expect(profile.id == 42)
    #expect(!profile.displayName.isEmpty)
}

Prefer parameterized tests to copied methods:

@Test(arguments: [0, 1, 12, 99])
func roundTrip(_ value: Int) throws {
    #expect(try Codec.decode(Codec.encode(value)) == value)
}

Use #expect(throws:) for failures and confirmation for callback counts. Convert one-shot callbacks to checked continuations when that mirrors the production abstraction. Never make an async mock call synchronously merely to simplify a test.

Read examples/example_basic_test.swift for standalone Swift Testing structure and parameterization. Read examples/example_async_test.swift for a strict-concurrency-safe async HTTP boundary.

Dependency doubles

Choose the smallest double that proves behavior:

  • Stub: returns configured data.
  • Spy: records messages for later assertions.
  • Fake: implements a lightweight in-memory behavior.
  • Mock: verifies a specific interaction contract; use sparingly.

Do not subclass URLSession, URLSessionDataTask, or other framework classes just to override one method. Their async convenience methods are not required to dispatch through callback overrides. Inject an HTTPTransport with an async requirement, or use a custom URLProtocol with an ephemeral session when URL loading itself is the subject.

Always complete every callback path exactly once. Validate malformed responses and HTTP status codes instead of treating every non-nil body as success.

XCTest-specific work

UI automation

  • Assign stable semantic identifiers in production views, such as login.username and login.success; do not query localized display text.
  • Wait for state transitions with waitForExistence(timeout:) or an NSPredicate expectation.
  • Set launch arguments and environment before launch() to make the backend and account state deterministic.
  • Mark UI tests @MainActor where required by the active SDK.
final class LoginUITests: XCTestCase {
    @MainActor
    func testSuccessfulLogin() {
        let app = XCUIApplication()
        app.launchArguments = ["-use-stub-auth"]
        app.launch()

        app.textFields["login.username"].tap()
        app.textFields["login.username"].typeText("sample")
        app.buttons["login.submit"].tap()

        XCTAssertTrue(app.staticTexts["login.success"].waitForExistence(timeout: 2))
    }
}

Performance

Use XCTest metrics and baselines instead of a one-shot Date threshold. Keep setup outside measure unless setup is intentionally measured.

measure(metrics: [XCTClockMetric(), XCTCPUMetric(), XCTMemoryMetric()]) {
    _ = processor.process(fixture)
}

Record the build configuration, device class, and baseline environment. Do not compare absolute timings across dissimilar CI workers.

Swift concurrency rules

  • Let Swift Testing run independent tests in parallel; use .serialized only for unavoidable shared external state.
  • Prefer actors or immutable Sendable values in doubles used across tasks.
  • Inject a clock rather than sleeping. Drive the test clock explicitly.
  • Verify cancellation and cleanup, not only successful completion.
  • Do not silence Sendable diagnostics with @unchecked Sendable unless the type has a documented synchronization invariant.

Review checklist

  • The test imports the production module or clearly labels a standalone teaching fixture; it never redeclares an imported production type.
  • The assertion checks behavior, not an implementation detail.
  • Every asynchronous path is awaited and bounded by the test runner.
  • No real network, wall-clock sleep, shared account, or random order controls the result.
  • Failure, empty-response, malformed-response, and cancellation paths are covered where applicable.
  • UI queries use identifiers and explicit waits.
  • Performance tests use metrics and baselines.
  • Availability and CI toolchain requirements are stated.

Supporting material

  • README.md summarizes framework selection and minimum tools.
  • examples/example_basic_test.swift is a dependency-free Swift Testing example.
  • examples/example_async_test.swift demonstrates async transport injection without URLSession subclassing.
  • examples/prompts.md contains realistic activation prompts.

When adapting an example to an app, move the sample system-under-test declaration into the production target and replace it in the test target with @testable import AppModule.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.