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

前端 未结 7 1429
抹茶落季
抹茶落季 2020-12-05 01:46

I would like my app to run special code (e.g. resetting its state) when running in UI Testing mode. I looked at environment variables that are set when the app is running fr

7条回答
  •  萌比男神i
    2020-12-05 02:29

    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.

提交回复
热议问题