/ios-swift-skills:swift-unit-testing-skillfrom ios-swift-skills
Designs, writes, migrates, and reviews Swift unit/integration tests using Swift Testing by default, with XCTest for UI automation, performance, and legacy suites. Covers @Test, #expect, #require, parameterized tests, async tests, DI, mocks, test plans, TDD, and flaky test handling.
Ships in ios-swift-skills. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them.
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.
XCUIApplication, XCTMetric, Objective-C tests, and incremental maintenance of existing XCTest suites.@Test methods into an XCTestCase subclass.| Goal | Default |
|---|---|
| Pure Swift unit or integration behavior | Swift Testing |
| Async/throws, parameterized cases, traits, tags | Swift Testing |
| UIKit/SwiftUI end-to-end interaction | XCTest UI testing |
| Runtime, CPU, memory, launch, or signpost metrics | XCTest performance APIs |
| Objective-C test code or legacy suite | XCTest |
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.
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.
Choose the smallest double that proves behavior:
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.
login.username and login.success; do not query localized display text.waitForExistence(timeout:) or an NSPredicate expectation.launch() to make the backend and account state deterministic.@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))
}
}
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.
.serialized only for unavoidable shared external state.Sendable values in doubles used across tasks.@unchecked Sendable unless the type has a documented synchronization invariant.URLSession subclassing.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.
What SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
swift-testing, xctest, unit-testing, integration-testing, tdd, test-architecture
14 skills in this plugin
/app-intents-widgetsDesign, implement, debug, and verify stable App Intents and WidgetKit integrations, including AppIntent, AppEntity, AppEnum, entity queries, AppShortcutsProvider, dependency injection, shared extension data, widget timelines, relevance, controls, Live Activity selection, deep links, handoff, confirmation, and privacy. Use when exposing app actions to Siri or Shortcuts, building configurable or interactive widgets, sharing state with extensions, or choosing the correct system surface for an action. Do not use for ordinary in-app buttons, generic SwiftUI layout, background scheduling without a widget or intent, SiriKit-only legacy work with no migration request, or Live Activity push-server implementation as the sole task./apple-evaluationsBuild, review, and debug generative AI quality regression suites with Apple's Evaluations framework, including Evaluation, datasets, metrics, Swift Testing integration, calibrated model judges, and tool-call trajectories. Use when measuring an Apple-platform intelligence feature, comparing prompts or models, or diagnosing misleading evaluation results. Do not use for ordinary deterministic unit tests, UI automation, performance profiling alone, model training, or an unrelated vendor's evaluation framework.npx claudepluginhub fal3/swift-apple-skills --plugin ios-swift-skillsOther plugins' skills for the same job