Swift Testing UI tests (Xcode 27.3 beta 2): launch() in the second test leaves the app "notRunning" (FB25692723)

Xcode 27.3 beta 2 lets us write UI tests with Swift Testing and XCUIAutomation, so I tried moving our UI tests over. In a UI testing bundle, when more than one Swift Testing test launches the app, XCUIApplication().launch() in the second test returns after about 0.5 seconds with state == .notRunning. Every element query in that test then fails.

The app process is actually spawned and keeps running, but XCUIAutomation never attaches to it: state stays .notRunning even after wait(for: .runningForeground, timeout: 5).

Minimal repro, in a new project with a SwiftUI app that shows a "Ready" text:

import Testing
import XCUIAutomation

@MainActor
struct SwiftTestingLaunchTests {
    @Test func `Launch 1`() throws { try launchAndCheck() }
    @Test func `Launch 2`() throws { try launchAndCheck() }
    @Test func `Launch 3`() throws { try launchAndCheck() }
    @Test func `Launch 4`() throws { try launchAndCheck() }

    private func launchAndCheck() throws {
        let app = XCUIApplication()
        app.launch()
        try #require(app.state == .runningForeground, "state right after launch(): \(app.state.rawValue)")
        try #require(app.staticTexts["Ready"].waitForExistence(timeout: 10))
    }
}

Result (same in 4 of 4 runs, parallel testing off):

✔ Test "Launch 1" passed after 4.784 seconds.
✘ Test "Launch 2" recorded an issue at SwiftTestingLaunchTests.swift:15:13: Expectation failed: app.state == .runningForeground
↳ state right after launch(): 1
✔ Test "Launch 3" passed after 5.516 seconds.
✔ Test "Launch 4" passed after 5.594 seconds.

The same 4 tests written with XCTest (XCTestCase, XCUIApplication().launch(), XCTAssertEqual(app.state, .runningForeground)) pass every time on the same simulator.

In the activity log, only the failing launch goes through "Wait for accessibility to load", right after terminating the previous instance:

Launch 2 (fails):
    t =     6.99s     Launch com.example.LaunchRepro
    t =     6.99s         Terminate com.example.LaunchRepro:17666
    t =     7.46s         Wait for accessibility to load
    t =     7.47s         Setting up automation session
Launch 3 (passes):
    t =     8.59s         Launch com.example.LaunchRepro
    t =     8.59s             Terminate com.example.LaunchRepro:17672
    t =     9.84s             Setting up automation session

What I tried, none of which helped:

  • calling terminate() before launch()
  • calling launch() again when state is not .runningForeground (this kills the instance that was starting)
  • calling activate() when state is not .runningForeground

In our real app (11 UI tests), the same 3 tests failed this way in every run, and each one passed when run alone. For now we keep the XCUIAutomation tests in XCTest and use Swift Testing only for tests that don't drive the UI.

Environment: Xcode 27.3 beta 2 (27C5035i), iOS 27.3 simulator (24C5049d), iPhone 18 Pro simulator.

Filed as FB25692723 with the minimal project attached.

Has anyone else seen this, or found a workaround?