When adopting Swift Testing in an iOS codebase with thousands of XCTest tests, do you migrate everything, run both side by side, or write only new tests in it?
answer
- one target can hold both
- what Swift Testing does not replace
- parallel and off the main actor
- assertions crossing frameworks
basics
~20 sUsually run both side by side: one test target holds XCTest classes and @Test functions, so new tests use Swift Testing, old ones migrate when touched, and UI and performance tests stay on XCTest, which Swift Testing does not replace.
solid answer
~50 sA big-bang migration buys little and risks a lot, and "new tests only" leaves two styles forever, so most teams run both side by side with rules. Both frameworks can live in one target and even one file. New unit tests are written with `@Test` and `#expect`; an XCTest class is migrated when someone changes its tests anyway, or in batches where it pays off. UI tests and `measure` performance tests stay on XCTest, because Swift Testing has no UI-automation or performance-measurement API. The rollout has traps: Swift Testing runs tests in parallel and off the main actor by default, `@Test` cannot be placed inside an `XCTestCase` subclass, and before Swift 6.4 an `XCTAssert` in a shared helper called from a `@Test` function was silently ignored. From 6.4 such cross-framework failures are reported, with a strength that depends on the package's tools version.
code
swift · 26 linesimport Foundation
import Testing
import XCTest
// Existing XCTest class: untouched, still runs.
final class RateCacheXCTests: XCTestCase {
func testCacheStartsEmpty() {
XCTAssertTrue(RateCache().isEmpty)
}
}
// New tests in the same target and the same file.
struct RateCacheTests {
@Test func storesRate() {
var cache = RateCache()
cache.store(code: "EUR", rate: 0.92)
#expect(cache.rate(for: "EUR") == 0.92)
}
}
struct RateCache {
private var rates: [String: Double] = [:]
var isEmpty: Bool { rates.isEmpty }
mutating func store(code: String, rate: Double) { rates[code] = rate }
func rate(for code: String) -> Double? { rates[code] }
}go deeper
Recall that XCTest and Swift Testing can live in the same test target, and that @Test functions cannot go inside an XCTestCase subclass.
Explain what changes when a class is converted: parallel execution, no default main-actor isolation, init and deinit for set-up and tear-down, and #require for stopping on failure.
Show how you would migrate one suite safely: shared helpers first, parallel-safety fixes, and how assertion failures cross the two frameworks on a 6.4 toolchain.
Own the strategy: weigh churn against consistency and CI time, set review rules and a toolchain floor, and decide which suites migrate early and which never move.
## The three options | Strategy | What it costs | What it buys | When it fits | |---|---|---|---| | Migrate everything now | weeks of churn, review load, new flakiness from parallel runs | one style, one set of helpers | small suites, or a team already rewriting its test infrastructure | | Side by side, migrate when touched | two styles for a while, helper duplication | new tests get the benefits at once, risk is spread over time | most large iOS codebases | | New tests only, never migrate | permanent two-style codebase | lowest effort now | suites that are stable and rarely edited | There is no single right answer; the decision depends on how often the old tests change, how much shared helper code they have, and how much CI time the team can spend on newly exposed races. ## What can and cannot mix Swift Testing and XCTest coexist at several levels, with hard edges: - **One target, one file.** A test target can hold `XCTestCase` subclasses and `@Test` functions; a file that has both imports both modules. `swift test` and Xcode run both. - **Not one class.** `@Test` cannot be applied to a method of an `XCTestCase` subclass; the macro rejects it at compile time. A migrated class becomes a separate suite type. - **Assertions across frameworks.** Swift 6.4 adds **interoperability**. Per Apple's migration guide, with a toolchain older than 6.4 an XCTest assertion inside a `@Test` function is ignored, so the test passes. With a 6.4 toolchain, it becomes a warning if the package's `swift-tools-version` is below 6.4 and a failure if it is 6.4 or later, with a hint to replace it with `#expect`. In every mode except `none`, a Swift Testing issue recorded inside an XCTest method keeps its severity. The `SWIFT_TESTING_XCTEST_INTEROP_MODE` environment variable selects `none`, `limited`, `complete` or `strict`. - **What Swift Testing does not replace.** It has no UI-automation API and no performance-measurement API, so UI tests and `measure` blocks stay on XCTest. ## Behaviour changes that bite during migration 1. **Parallel by default.** XCTest runs a class's tests one after another by default; Swift Testing runs tests in parallel, in one process. Hidden shared state (singletons, `UserDefaults.standard`) shows up as new flakiness. 2. **Not on the main actor.** XCTest runs synchronous test methods on the main actor; Swift Testing runs test functions on an arbitrary task. Main-actor code needs `@MainActor` on the test. 3. **Set-up and tear-down.** Hooks become `init()` and, for a class or actor suite, `deinit`, which run for each test on a fresh instance. 4. **Stopping on failure.** `try #require` replaces the stop-after-failure setting and unwrapping helpers. 5. **Asynchronous expectations.** Awaiting `async` code directly, or `confirmation()` for event counts, replaces waiting on expectation objects; confirmations never wait or time out. ## A rollout plan a lead can defend 1. Agree that every new unit test is written with Swift Testing, and add the rule to review guidelines. 2. Rewrite shared assertion helpers with `#expect` first, since those are what both frameworks call; on a 6.4 toolchain, interop then reports their failures in XCTest tests too. 3. Raise the package's tools version to 6.4, or set the interop mode explicitly, so an XCTest assertion inside a `@Test` function can no longer pass silently. 4. Migrate a class when its tests are edited anyway; migrate in batches only where parallel runs give a measurable CI speed-up. 5. Leave UI and performance tests on XCTest and say so in the guidelines, so nobody tries to convert them. 6. Track flakiness during the rollout; a newly flaky migrated suite usually means shared state, not a framework bug. ## The judgment calls - **Churn versus consistency.** Rewriting thousands of passing tests rarely finds bugs; it mostly moves risk around. - **CI time.** Parallel execution can shorten runs a lot, which is the strongest argument for migrating hot, slow suites early. - **Toolchain floor.** Swift Testing needs Xcode 16 or later, and interop needs a 6.4 toolchain; a team pinned to older Xcode versions has to wait or accept the silent-pass risk. - **Skills.** Parameterized tests and traits change how people write tests; budget time for review and examples rather than a mechanical conversion.
- A shared helper calls XCTAssertEqual. What happens when a Swift Testing test calls that helper and the values differ?With a toolchain older than 6.4 the XCTest failure is ignored and the Swift Testing test passes. With 6.4 it is reported: as a warning when the package's `swift-tools-version` is below 6.4, as a failure at 6.4 or later, plus a hint to use `#expect`. `SWIFT_TESTING_XCTEST_INTEROP_MODE` overrides the default. Rewriting the helper with `#expect` avoids the question entirely.
- Which tests should stay on XCTest during the rollout?UI tests, because UI automation runs on XCUITest and Swift Testing has no equivalent, and performance tests built on `measure` blocks, for the same reason. Tests that rely on custom `XCTestCase` base-class infrastructure can also wait until that infrastructure has a Swift Testing replacement, such as suite `init()` and shared helpers.
- Why do some migrated suites become flaky right after conversion?XCTest ran their methods one after another, while Swift Testing runs tests in parallel by default and off the main actor. Hidden shared state, such as singletons or `UserDefaults.standard`, and main-actor assumptions now surface. The fix is per-test state in `init()`, `@MainActor` where needed, and `.serialized` on a suite only as a stopgap.
- Swift Testing and XCTest tests need separate test targets.
- You can add @Test methods to an existing XCTestCase subclass.
- Migration is a mechanical rename, so converting everything at once is free.
- Swift Testing replaces UI tests and measure-based performance tests too.
- Converted tests behave the same because both frameworks run tests sequentially.
Assumes a Swift 6.4 toolchain; XCTest–Swift Testing interoperability arrived in 6.4, with the default mode set by the toolchain and the package's swift-tools-version.