Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 from UI Testing and there aren't any obvious parameters to differentiate between the app running normally vs in UI Testing. Is there a way to find out?

Two workarounds that I'm not satisfied with are:

  1. Set XCUIApplication.launchEnvironment with some variable that I later check in the app. This isn't good because you have to set it in the setUp method of each test file. I tried setting the environment variable from the scheme settings but that doesn't propagate to the app itself when running UI Testing tests.
  2. Check for the lack of existence of the environment variable __XPC_DYLD_LIBRARY_PATH. This seems very hacky and might only be working now because of a coincidence in how we have our target build settings set up.
like image 769
Liron Yahdav Avatar asked Aug 31 '15 21:08

Liron Yahdav


People also ask

How do I test my iOS UI?

When you're ready to test, go to a test class and place the cursor inside the test method to record the interaction. From the debug bar, click the Record UI Test button. Xcode will launch the app and run it. You can interact with the element on-screen and perform a sequence of interactions for any UI test.

How do I test my UI app?

Checklist for Testing Mobile App UITest overall color scheme and theme of the app on the device. Check the style and color of icons. Test the look and feel of the web content across a variety of devices and network conditions.

What is UI testing in Xcode?

UI testing allows you to automate this procedure, so every time you update your code, Xcode will automatically test whether your app works correctly without you having to manually do it.

What is UI Automation on Iphone?

UI Automation is a tool that Apple provides and maintains for higher level, automated, testing of iOS applications. Tests are written in JavaScript, adhering to an API defined by Apple. Writing tests can be made easier by relying on accessibility labels for user interface elements in your application.


1 Answers

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.

like image 57
jnic Avatar answered Oct 01 '22 05:10

jnic