Enable parallel UI test execution via per-session data isolation

Each test class now gets a unique session ID (UUID) passed to the app
via UI_TEST_SESSION_ID environment variable. The app uses this to:

- Route GroupUserDefaults to a session-specific UserDefaults suite,
  preventing tests from clobbering each other's AppStorage state
- Create an in-memory SwiftData container instead of the shared
  on-disk App Group store, eliminating SQLite contention

Refactored 8 test classes that bypassed BaseUITestCase.setUp() with
custom launch args — they now use overridable `localeArguments` and
`extraLaunchArguments` properties, keeping session ID injection
centralized. Added `relaunchApp(resetState:bypassSubscription:)` to
BaseUITestCase for tests that need mid-test relaunch with different
subscription state.

Includes a ParallelUITests.xctestplan with class-level parallelism
enabled and random execution ordering.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Trey T
2026-03-24 15:04:55 -05:00
parent 8231750cff
commit 2ef1c1ec51
13 changed files with 128 additions and 145 deletions

View File

@@ -36,6 +36,15 @@ enum SharedModelContainer {
/// - Returns: Configured ModelContainer
/// - Throws: SharedModelContainerError if creation fails
static func create(useCloudKit: Bool = true) throws -> ModelContainer {
// When UI testing, use in-memory storage for parallel test isolation.
// Each test process gets its own empty container no shared on-disk state.
// Check ProcessInfo directly to avoid depending on UITestMode (not in widget targets).
if ProcessInfo.processInfo.arguments.contains("--ui-testing") {
let schema = Schema([MoodEntryModel.self])
let config = ModelConfiguration(schema: schema, isStoredInMemoryOnly: true, cloudKitDatabase: .none)
return try ModelContainer(for: schema, configurations: [config])
}
let schema = Schema([MoodEntryModel.self])
let storeURL = try Self.storeURL

View File

@@ -25,7 +25,26 @@ struct Constants {
}
struct GroupUserDefaults {
/// Whether the current process is a UI test session with an isolation ID.
/// Inlined from ProcessInfo to avoid depending on UITestMode (which isn't in widget targets).
private static var uiTestSessionID: String? {
guard ProcessInfo.processInfo.arguments.contains("--ui-testing") else { return nil }
return ProcessInfo.processInfo.environment["UI_TEST_SESSION_ID"]
}
/// The suite name currently in use. Used by resetAppState() to clear the correct domain.
static var currentSuiteName: String {
if let sessionID = uiTestSessionID {
return "uitest.\(sessionID)"
}
return Constants.currentGroupShareId
}
static var groupDefaults: UserDefaults {
// When UI testing with a session ID, use a per-session suite for parallel isolation.
if let sessionID = uiTestSessionID {
return UserDefaults(suiteName: "uitest.\(sessionID)") ?? .standard
}
#if DEBUG
return UserDefaults(suiteName: Constants.groupShareIdDebug) ?? .standard
#else

View File

@@ -42,6 +42,12 @@ enum UITestMode {
ProcessInfo.processInfo.arguments.contains("--expire-trial")
}
/// Unique session ID for parallel test isolation.
/// Each test class gets its own session, ensuring no shared state between parallel test runners.
static var sessionID: String? {
ProcessInfo.processInfo.environment["UI_TEST_SESSION_ID"]
}
/// Seed fixture name if provided (via environment variable)
static var seedFixture: String? {
ProcessInfo.processInfo.environment["UI_TEST_FIXTURE"]
@@ -93,8 +99,9 @@ enum UITestMode {
@MainActor
private static func resetAppState() {
let defaults = GroupUserDefaults.groupDefaults
// Clear group user defaults using the suite domain name
defaults.removePersistentDomain(forName: Constants.currentGroupShareId)
// Clear group user defaults using the session-specific or shared suite domain name
let suiteName = GroupUserDefaults.currentSuiteName
defaults.removePersistentDomain(forName: suiteName)
// Explicitly clear subscription cache keys that may survive removePersistentDomain
// on app group suites (known reliability issue).