How to detect if iOS app is running in UI Testing mode

I’ve been researching this myself and came across this question. I ended up going with @LironYahdav’s first workaround:

In your UI test:

- (void)setUp
{
    [super setUp];

    XCUIApplication *app = [[XCUIApplication alloc] init];
    app.launchEnvironment = @{@"isUITest": @YES};
    [app launch];
}

In your app:

NSDictionary *environment = [[NSProcessInfo processInfo] environment];
if (environment[@"isUITest"]) {
    // Running in a UI test
}

@JoeMasilotti’s solutions are useful for unit tests, because they share the same runtime as the app being tested, but are not relevant for UI tests.

Leave a Comment