Make Shared Test Helpers Fail In XCTest And Swift Testing

You move a test from XCTest to Swift Testing. Its assertions live in a shared helper, so the new test still calls that helper. It compiles, runs, and passes.

Then you give it deliberately invalid input. It passes again.

Before trusting a migrated helper, prove that it can fail through both frameworks. Swift 6.4 and Xcode 27 provide interoperability for this, but the reporting mode matters. An old package manifest can make an XCTest failure inside a Swift Testing test produce warnings while the command exits successfully.

Anastasia Petrova’s account of Depop’s test migration makes the practical problem clear: converted and unconverted tests can share helpers for months. Their migration needed a framework-aware reporting adapter before the new interoperability arrived. We’ll build a smaller, current-toolchain check: one thumbnail assertion helper, one invalid size, and failure reports from both runners.

Download the complete example and checks.

Start with a failure you can recognize

Use a disposable Swift package for the experiment. Put this in Package.swift:

// swift-tools-version: 6.4
import PackageDescription

let package = Package(
    name: "SharedHelperChecks",
    platforms: [.macOS(.v14)],
    targets: [.testTarget(name: "SharedHelperTests")]
)

Run this experiment on a Mac with Xcode 27 and its Swift 6.4 toolchain or later. The manifest declares the minimum tools version for the package. We’ll change that line while using the same installed compiler.

Put this old helper in Tests/SharedHelperTests/LegacyHelper.swift. Imagine it verifies the dimensions returned by an image exporter:

import XCTest

nonisolated struct PixelSize: Sendable {
    let width: Int
    let height: Int
}

nonisolated func assertThumbnailSize(
    _ size: PixelSize,
    maximumEdge: Int = 960,
    file: StaticString = #filePath,
    line: UInt = #line
) {
    XCTAssertGreaterThan(
        size.width, 0, "Width must be positive", file: file, line: line)
    XCTAssertGreaterThan(
        size.height, 0, "Height must be positive", file: file, line: line)
    XCTAssertLessThanOrEqual(
        max(size.width, size.height), maximumEdge,
        "Thumbnail exceeds its pixel budget", file: file, line: line)
}

The dimensions are values, so these helpers do not need actor isolation. The explicit nonisolated declarations also keep them usable when the test target defaults to MainActor isolation. That choice says nothing about whether a real image exporter needs the main actor.

Now create Tests/SharedHelperTests/Checks.swift:

import Testing
import XCTest

@Test nonisolated func legacySwiftFailure() {
    assertThumbnailSize(PixelSize(width: 640, height: 0))
}

nonisolated final class LegacyThumbnailTests: XCTestCase {
    func testLegacyFailure() {
        assertThumbnailSize(PixelSize(width: 640, height: 0))
    }
}

These are deliberately failing tests. A zero-height thumbnail is invalid. Keep the experiment separate from a normal passing suite.

From the package directory, run each caller separately:

swift test --filter legacySwiftFailure
swift test --filter LegacyThumbnailTests.testLegacyFailure

Check that the requested test actually ran, that it was reported as failed, and that the command returned a nonzero exit status. A compiler error or an empty test selection is not the failure we’re looking for.

The manifest can change the result

With the installed Swift 6.4.2 compiler, the XCTest caller failed as expected. The Swift Testing caller depended on the manifest:

Manifest’s tools versionDefault modeOld helper called by @TestCommand exit
6.3LimitedPassed with two warnings0
6.4CompleteFailed, with a modernization warning1

To reproduce the first row, change only the manifest’s first line to // swift-tools-version: 6.3 and rerun the first command. Restore 6.4 afterward. No older compiler is involved in that comparison.

The successful exit in Limited mode is the trap. Compiler -warnings-as-errors does not promote these runtime test warnings into failures; the probe used that compiler flag and still exited zero.

Apple documents the package defaults and interoperability modes: Complete preserves failure severity in both directions; Limited downgrades failures originating in XCTest; None disables interoperability; Strict terminates on cross-framework XCTest issues. These modes concern assertions reaching the other runner, not whether the project contains both frameworks.

For an Xcode project, inspect Swift Testing and XCTest Interoperability in the test plan’s Test Execution settings. Apple’s WWDC26 migration session explains that older test plans inherit Limited mode, while new projects use Complete. Choose Complete when keeping existing helpers during migration. Strict is a more disruptive diagnostic option, not necessary for this example.

For this package, a 6.4 manifest selects Complete automatically. If you need to keep a 6.3 manifest for consumers, try SWIFT_TESTING_XCTEST_INTEROP_MODE=complete swift test --filter legacySwiftFailure on a Swift 6.4 runner. With that override, the zero-height case failed at the caller even with the 6.3 manifest. A CI machine running a pre-6.4 toolchain lacks this bridge. Keep assertions native to each runner there until you can upgrade.

Move the helper to Swift Testing

Complete mode makes the old helper report a real failure. We can now modernize the helper itself while leaving its XCTest callers in place.

Add Tests/SharedHelperTests/ThumbnailHelper.swift:

import Testing

nonisolated func expectThumbnailSize(
    _ size: PixelSize,
    maximumEdge: Int = 960,
    sourceLocation: SourceLocation = #_sourceLocation
) {
    #expect(
        size.width > 0, "Width must be positive",
        sourceLocation: sourceLocation)
    #expect(
        size.height > 0, "Height must be positive",
        sourceLocation: sourceLocation)
    #expect(
        max(size.width, size.height) <= maximumEdge,
        "Thumbnail exceeds its pixel budget",
        sourceLocation: sourceLocation)
}

The checks still mean the same thing: positive dimensions, with the longer edge no larger than the budget. #expect records a failed condition and continues, allowing independent problems to be reported together.

The SourceLocation parameter matters. Its default captures the helper’s call site, and every expectation forwards that location. Without the forwarding, several different callers can all appear to fail on the same line inside the helper. If you add another wrapper around this one, that wrapper should accept and forward the location too.

For a helper that unconditionally reports an invalid branch, use Issue.record. For a missing value that makes the remaining test impossible, use throwing #require. The Stack Overflow question about replacing XCTFail already has answers covering both choices. The extra concern here is proving that the report reaches each framework that calls the helper.

Append these callers to Checks.swift:

@Test nonisolated func modernSwiftFailure() {
    expectThumbnailSize(PixelSize(width: 640, height: 0))
}

@Test nonisolated func modernSwiftSuccess() {
    expectThumbnailSize(PixelSize(width: 640, height: 480))
}

nonisolated final class ModernThumbnailTests: XCTestCase {
    func testModernFailure() {
        expectThumbnailSize(PixelSize(width: 640, height: 0))
    }

    func testModernSuccess() {
        expectThumbnailSize(PixelSize(width: 640, height: 480))
    }
}

Run the invalid and valid cases separately:

swift test --filter modernSwiftFailure
swift test --filter ModernThumbnailTests.testModernFailure
swift test --filter modernSwiftSuccess
swift test --filter ModernThumbnailTests.testModernSuccess

The first two commands should fail. The last two should pass. The invalid cases should identify the zero height and the caller’s line in Checks.swift, rather than a line in ThumbnailHelper.swift.

That positive control matters too. An always-failing helper is excellent at finding problems, including imaginary ones.

Keep the migration boundary small

This is targeted assertion interoperability, introduced in Swift 6.4’s ST-0021. It does not make every XCTest API suitable for Swift Testing. In particular, do not carry XCTestExpectation and XCTWaiter into an async Swift Testing test and assume the reporting mode fixes their execution model. Await controlled work when migrating those tests. Keep UI automation in XCTest.

It also does not make shared mutable mocks independent. If migration exposes failures between parallel tests, inspect the fixture’s ownership separately; registered defaults are one concrete example.

Verification. The package targets macOS 14. I ran the examples with SwiftPM on macOS 27.2.1 using Xcode 27.2 and Swift 6.4.2. The checks cover both manifest versions, ordinary and MainActor-default isolation, valid and invalid inputs, and caller locations. The Limited-mode test passed for a zero-height thumbnail. After migrating the helper, both runners failed that input at the caller and accepted the valid size.

For each shared helper you migrate, retain one representative bad input long enough to check the failure report from both callers. Then remove the deliberate failure fixture from the normal suite. A passing migration should mean the tests still know how to complain.