25

So, i have already existing project at my hands and i’m trying to create some UI tests by using this new fancy UI Testing Bundle provided by apple. The problem is that test target doesn't have access to any external framework (and i need to do some setup with one of them). Adding framework in build phases and coping framework search path from main target doesn't do anything.

After day of browsing i found out only one thing, that ”makes things kinda different”. By setting up Bundle Loader and Test Host to $(BUILT_PRODUCTS_DIR)/App.app/App , i still couldn't import external frameworks to test.m, but i could import classes that do that for them self. And it all would be fine and dandy unless it didn't break some stuff. By setting Bundle and Host now my UI test is unable to execute launch method:

[[[XCUIApplication alloc] init] launch];

It crashes with error: Assertion Failure: UI Testing Failure - App state is still not terminated.

In the end i could remove launch method from setup and trigger every single test manually, so it restarts application every time before executing, but this solution seems so wrong (especially for some bigger projects). Does anyone know proper way to handle this problem?

Senseful
  • 86,719
  • 67
  • 308
  • 465
BigSzu
  • 271
  • 2
  • 8
  • This is also applicable to OS X applications, and a answer is needed for all Apple developers to take advantage of this feature in El Capitan. – Claus Jørgensen Jul 30 '15 at 13:00

1 Answers1

1

What I've done for this is add an environment variable to XCUIApplication to specify UI tests are being run. I then have a pre-processor check for #DEBUG in a main part of the application, and then check if the test environment variable has been set; if it has, do the necessary steps for the UI tests.

Essentially, this will allow you to configure your app to how you need it for the UI tests to run. It also means the pre-processor check will strip out that setup code entirely for the release build.

Harry
  • 3,076
  • 4
  • 28
  • 44
  • That'll work if you're making a small hobby app with no noteworthy amount of data to fake out. It's not a feasible approach for real-world app testing, think Facebook/Instagram style testing, but with fake data. – Claus Jørgensen Aug 03 '15 at 11:02
  • Depends what you mean by "fake data". You could easily load in a pre-built sqlite database this way. Of course, you may not be getting the full effect of UI testing if you're faking everything. – Harry Aug 03 '15 at 15:29
  • If your imagination is that software applications is just presenting cached data, this is going to be hard to explain. Think of login, interactive chats, calling, video, audio, opening/closing tabs, windows, changing settings, colours, accessibility, voiceover, etc. And each test written *in isolation*, which requires a reset of the available and expected data in each test. This is why we need access to the same data as Unit Tests, otherwise the framework is useless compared to KIF on iOS (and nothing on OS X) – Claus Jørgensen Aug 04 '15 at 05:58