Swift Testing with AI agents: write unit tests, migrate from XCTest and read the results
Tell the agent to write new unit tests with Swift Testing (@Test, #expect, #require) and to keep UI and performance tests on XCTest, as Apple recommends. Migrate XCTest suites one file at a time with Apple's mapping, check the interoperability mode, and make the agent read the .xcresult instead of guessing.
Last updated:
This page is about getting a coding agent (Claude Code, Cursor, Codex, or the agents inside Xcode) to write Swift unit tests with Swift Testing, migrate existing XCTest suites, and run and read the tests itself. UI tests with XCUITest are a separate job. Every testing tool in the catalog is listed under Testing.
What Apple says: Swift Testing or XCTest?
Apple's XCTest page has a tip that settles most arguments with an agent:
Xcode 16 and later include Swift Testing; "consider using Swift Testing for new unit test development and migrating existing tests".
"A test target can contain tests using both Swift Testing and XCTest, however don't mix API from the two frameworks in the same test."
"Continue to use XCTest for user interface tests and performance tests."
Swift Testing itself, in Apple's words, defines tests "almost anywhere with a single attribute", groups them with Swift types, parameterizes them across inputs, enables them by runtime conditions, categorizes them with tags, and runs them in parallel in-process.
Rules worth adding to AGENTS.md or CLAUDE.md:
Tests: new unit tests use Swift Testing (
import Testing,@Test,#expect,#require). UI and performance tests stay XCTest. Never callXCTAssert*inside a@Testfunction or#expectinside anXCTestCase. Prefer#requirewhen the rest of the test depends on a value. After every change, run only the affected tests withxcodebuild test -only-testing …and report the result from the.xcresult, not from memory.
Step 1: write new tests the Swift Testing way
What Apple's reference gives the agent to work with:
#expectcaptures the expression and reports what failed; the test keeps running.#requirethrows (ExpectationFailedError) and stops the test, and with an optional it unwraps the value:let customer = try #require(Customer(id: 123)).Parameterized tests: pass collections to
@Test(arguments:)instead of looping inside the test, so a failure names the input. The test cases run in parallel by default.Async code: mark the test
asyncandawaitthe call. For events you can't await, useconfirmation(); setexpectedCount(or a range) when the event fires more than once.Parallelism: tests run in parallel by default, in the same process.
.serializedon a suite runs its tests one after another; on a parameterized test it runs the cases one after another; on a plain test it has no effect.
swift
import Testing
@testable import MyApp
@Suite struct OrderCalculatorTests {
@Test(arguments: [0, 1, 5])
func subtotalIsNeverNegative(pizzas: Int) async throws {
let calculator = OrderCalculator()
let subtotal = try #require(await calculator.subtotal(forPizzaCount: pizzas))
#expect(subtotal >= 0)
}
}Step 2: migrate XCTest suites with Apple's mapping
Apple's "Migrating a test from XCTest" is the rulebook to hand the agent. The key moves:
| XCTest | Swift Testing |
|---|---|
import XCTest | import Testing (import both if a file mixes tests) |
class …: XCTestCase | a suite: any type with @Test functions; Apple recommends a struct or actor |
setUp() / tearDown() | init() / deinit (needs a class or actor) |
func testSomething() | @Test func something() (no test prefix needed) |
XCTAssertEqual(x, y) | #expect(x == y) |
XCTAssertThrowsError(try f()) | #expect(throws: (any Error).self) { try f() } |
try XCTUnwrap(x) | try #require(x) |
XCTFail("…") | Issue.record("…") |
continueAfterFailure = false | #require |
XCTestExpectation | confirmation() |
XCTSkip / XCTSkipIf | a ConditionTrait such as .enabled(if:) before the test starts, or Test.cancel once it is running |
XCTExpectFailure | withKnownIssue { … } |
Two behavior changes the agent must handle, from the same page:
Parallel by default. XCTest runs a suite's tests sequentially; Swift Testing runs them in parallel. Tests that share state such as global variables can become unreliable; add
.serialized.No main actor by default. XCTest runs synchronous test methods on the main actor; Swift Testing runs test functions on an arbitrary task. Add
@MainActorwhere a test needs the main thread.
Apple also notes there is no equivalent of XCTAssertEqual(_:_:accuracy:); it points to isApproximatelyEqual() from Swift Numerics.
Step 3: check the interoperability mode before you trust a green run
Interoperability lets XCTest assertions work inside Swift Testing tests and Swift Testing expectations work inside XCTests, so shared helpers can migrate gradually. Apple documents four modes (none, limited, complete, strict) and how a failing XCTFail inside a @Test is reported:
| Mode | Failing XCTFail in a @Test |
|---|---|
none | no message |
limited | ⚠️ warning (plus a warning to replace the XCTest API) |
complete | ❌ failure (plus a warning to replace the XCTest API) |
strict | fatalError (replace the XCTest API) |
The default depends on the toolchain and the package's swift-tools-version: none below toolchain 6.4; from 6.4, limited with tools version below 6.4 and complete with 6.4 or later. Set it explicitly with the SWIFT_TESTING_XCTEST_INTEROP_MODE environment variable. In Xcode 27, a failing assertion called from the opposite framework shows a runtime issue with warning severity, controlled by the new Swift Testing and XCTest Interoperability setting in the test plan (release note 170335449).
What that means for an agent: in none mode, a helper that still calls XCTAssertEqual can let a broken @Test pass. Either finish migrating helpers to #expect, or run in complete mode while you migrate.
Step 4: let the agent run tests and read the results
From Apple's "Running tests and interpreting results":
sh
xcodebuild test -scheme SampleApp
xcodebuild test -scheme SampleApp -only-testing SampleAppTests/SampleAppTests
xcodebuild test -scheme SampleApp -only-testing SampleAppTests/SampleAppTests/testEmptyArrayWhenNoOverlappingNotes -run-tests-until-failure -test-iterations 20Identifiers take the form test_target/test_type/test_function (a suite: test_target/test_suite). Each xcodebuild test run writes an .xcresult bundle with session results, code coverage if enabled, and logs. Swift Testing captures the values of variables used in expectation macros and the arguments passed to a test function, and Xcode shows them in the failure details.
Xcode's own MCP server has build and test tools (in Xcode 27 they send dynamic activity status to external MCP clients, 181078347), and Xcode 27.2 beta 2 notes added a GetCodeCoverage MCP tool that "reports code coverage from the most recent test result, optionally filtered by target or file" (181141715). The final Xcode 27.2 release notes checked on 3 Nov 2026 do not repeat that line. Setup: Best MCP servers for Xcode. Outside Xcode, Peekie and xcforge turn the .xcresult into JSON an agent can read.
Swift Testing tools for agents compared
| Tool / skill | What it does (from README) | Agents | Install (from README) | License | ★ (27 Oct) | Last push |
|---|---|---|---|---|---|---|
| Swift Testing Expert (AvdLee) | Skill for modern Swift Testing APIs, XCTest migration, parameterized tests, traits and tags, async testing and parallel execution | any Agent Skills tool; Claude Code plugin | npx skills add https://github.com/avdlee/swift-testing-agent-skill --skill swift-testing-expert | MIT | 476 | 14 Sep 2026 ⚠ |
| Swift Testing Pro (twostraws) | Skill for @Test, #expect, #require, parameterized tests, traits, exit tests and confirmations | Claude Code, Codex, Cursor, Gemini | npx skills add https://github.com/twostraws/swift-testing-agent-skill --skill swift-testing-pro | MIT | 471 | 17 May 2026 ⚠ |
| Swift Testing Skill (farkasseb) | Targets what agents get wrong: tests that pass when they should fail (Task { } in a test, asserting in a callback, XCTAssert* in @Test), .serialized scope, invented APIs, Swift 6.1–6.4 additions; XCTest migration mapping | Claude Code, Codex | git clone https://github.com/farkasseb/swift-testing-skill ~/.agents/skills/swift-testing (Claude Code: plus mkdir -p ~/.claude/skills and ln -s ~/.agents/skills/swift-testing ~/.claude/skills/swift-testing) | MIT | 0 | 19 Oct 2026 |
| Swift Testing Expert (SwiftyJourney) | Writing, reviewing and migrating tests: syntax and traits, incremental XCTest migration with an interop check, hand-rolled test doubles, deterministic async tests, @MainActor/Sendable in tests, TDD | Claude Code, Cursor, Windsurf | npx skills add SwiftyJourney/swift-testing-expert-skill | MIT | 0 | 10 Aug 2026 ⚠ |
| SwiftTesting Skill (ibru) | Opinionated TDD/BDD style: when…_then… names, makeSUT factories, deterministic test doubles, no sleeps; Swift Testing only, leaves XCTest code alone unless asked | Claude Code, Cursor, Codex | npx skills add ibru/swift-testing-skill | MIT | 0 | 29 Jul 2026 ⚠ |
| test-kit (jyotiraditya-chauhan) | Flutter-first, covers several stacks. Plugin whose swift-testing skill writes Swift Testing (XCTest where it exists) and snapshot tests, asks what to test first; optional fault-injection self-check; five more skills for other stacks | Claude Code | /plugin marketplace add jyotiraditya-chauhan/test-kit, /plugin install testing-suite@test-kit-marketplace | MIT | 6 | 20 Aug 2026 ⚠ |
| iOS Testing (ioloro) | Skill for Swift Testing (default for new unit tests), XCTest performance tests and XCUITest, plus an iostesting CLI and an optional hook that blocks raw xcodebuild test | Claude Code | npm install -g @ioloro/ios-testing | MIT | 24 | 5 Aug 2026 ⚠ |
| Peekie (dodobrands) | CLI for .xcresult bundles: tests, warnings, errors, coverage, attachments as JSON; ships a Claude Code plugin and a Cursor rule | Claude Code, Cursor | mise use github:dodobrands/peekie, then /plugin marketplace add dodobrands/Peekie, /plugin install peekie | Apache-2.0 | 32 | 26 Oct 2026 |
| xcforge (justinthevoid) | MCP server and CLI (104 tools); test tools parse .xcresult: pass/fail counts, failures with source location, coverage, test list, test plan inspection, known-failures gating | Claude Code, Cursor, VS Code, Windsurf, any MCP client | brew tap justinthevoid/tap && brew install xcforge, then claude mcp add xcforge -- xcforge | MIT | 2 | 10 Oct 2026 |
| CRAP Check (n0an) | Covers several stacks (not Swift-only). Ranks functions by complexity versus test coverage (Swift coverage from llvm-cov or an .xcresult via xccov) and drives the score down with missing tests; exit code for CI | Claude Code, Codex, Cursor, Gemini | npx skills add https://github.com/n0an/crap-check --skill crap-check | MIT | 2 | 9 Sep 2026 ⚠ |
⚠ = no push for more than 4 weeks. Five of the six new tools have 0 to 2 stars; treat them as young projects and check that a new test fails when the code is broken before you trust it. Also in the catalog: SwiftData Testing for SwiftData test fixtures, and simless, which runs unit and XCUITest tests without booting a simulator.
Which one to pick:
One skill for writing and migrating tests: Swift Testing Expert (AvdLee) or Swift Testing Pro; add the farkasseb skill if your agent keeps writing tests that pass for the wrong reason.
Your team does TDD: SwiftTesting Skill (ibru) or Swift Testing Expert (SwiftyJourney); test-kit if you also want a fault-injection check on request.
The agent should run tests and read results: Peekie for
.xcresultto JSON, xcforge as a full MCP server, Xcode's own MCP server if Xcode is open.Decide where tests are missing: CRAP Check.
A prompt that keeps the agent honest
Setting up the agent itself: Claude Code for iOS development, Codex for iOS development or Cursor rules for SwiftUI.
Migrate
CartTests.swiftfrom XCTest to Swift Testing following Apple's "Migrating a test from XCTest". Keep every assertion, use a struct suite withinit()instead ofsetUp(),#requireinstead ofXCTUnwrap, andconfirmation()instead ofXCTestExpectation. Add.serializedif tests share state and@MainActorwhere the old test needed the main thread. Don't touch UI tests. Then run only this suite withxcodebuild test -only-testing, break one assertion on purpose to prove the suite still fails, revert it, and report the results from the.xcresult.
FAQ
- Can Claude Code or Codex migrate XCTest tests to Swift Testing?
- Yes, with Apple's mapping as the rulebook: replace import XCTest with import Testing, turn XCTestCase subclasses into suites (Apple recommends a struct or actor), setUp into init, tearDown into deinit, test methods into @Test functions, XCTAssert calls into #expect, XCTUnwrap into #require and XCTestExpectation into confirmation(). Migrate one file at a time and run the tests after each one.
- Should UI tests move to Swift Testing too?
- No. Apple's XCTest page says to consider Swift Testing for new unit tests and to continue using XCTest for user interface tests and performance tests. A test target can contain tests from both frameworks, but Apple advises against mixing API from the two frameworks in the same test.
- Why do migrated tests suddenly fail or flake?
- Two common reasons from Apple's migration guide. Swift Testing runs tests in parallel by default, while XCTest runs a suite's tests one after another, so tests that share global state can become unreliable; mark such suites .serialized. And XCTest runs synchronous test methods on the main actor by default, while Swift Testing runs test functions on an arbitrary task; add @MainActor where a test needs the main thread.
- What is the Swift Testing and XCTest interoperability mode?
- Interoperability lets XCTest assertions work in Swift Testing tests and Swift Testing expectations work in XCTests. Apple documents four modes: none, limited, complete and strict. The default depends on the toolchain and swift-tools-version, and you can set it with the SWIFT_TESTING_XCTEST_INTEROP_MODE environment variable. Xcode 27 adds a matching setting to the test plan.
- How does an agent run one Swift test and read the result?
- With xcodebuild test and the -only-testing option, using an identifier of the form test_target/test_type/test_function. The run writes an .xcresult bundle with results, code coverage if enabled, and logs. Peekie and xcforge turn that bundle into JSON for the agent, and Xcode 27.2 beta 2 notes added a GetCodeCoverage tool to Xcode's MCP server. The final Xcode 27.2 release notes checked on 3 Nov 2026 do not repeat that line.