Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I reset the application data after each test with Xcode 7 UI Testing?

Tags:

Apple introduced in Xcode 7 new UI Testing but I have a struggle whenever the tests launches the app, it starts with data that the application had before. It means tests cannot be independent and can be influenced by other tests.

It is not possible to access user defaults and other data because the application that is running tests has no access to the bundle of the tested application. Scripts are also out of question because they can be run before or after testing. And there is no way how to execute NSTask on iOS to run a script before each test suite.

Is there a way how do reset the application data before each test suite?

like image 880
Tomáš Linhart Avatar asked Sep 02 '15 10:09

Tomáš Linhart


People also ask

How do I delete a test in Xcode?

You can either right click the test and delete or select the target then select the minus symbol ( - ) at the bottom of the Document Navigator pane. Hope this helps.

How do I test UI in Xcode?

If your app doesn't already have a UI testing target, all you have to do to add one is to go to File > New > Target.. in Xcode and select a “UI testing bundle”.

What is XCTest in Xcode?

Overview. Use the XCTest framework to write unit tests for your Xcode projects that integrate seamlessly with Xcode's testing workflow. Tests assert that certain conditions are satisfied during code execution, and record test failures (with optional messages) if those conditions aren't satisfied.

How do I run all test cases in Xcode?

Using ⌃⌘U will run all the test cases without building the test target. It is the opposite of ⇧⌘U. This is especially useful when you want to skip a slow-building test target and jump straight into running all the test cases.


1 Answers

Not in a straight forward manner. But there are some workarounds.

The XCUIApplication can set command line arguments and environment variables that can alter your application’s behavior.

A simple example of your main.m file:

int main(int argc, char * argv[]) { #if DEBUG     // Reset all data for UI Testing     @autoreleasepool {         for (int i = 1; i < argc; ++i) {             if (0 == strcmp("--reset-container", argv[i])) {                 NSArray *folders = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);                 NSFileManager *fm = [[NSFileManager alloc] init];                 for (NSString *path in folders) {                     [fm removeItemAtPath:path error:nil];                 }                 // Also remove documents folder if necessary...             }         }     } #endif     @autoreleasepool {         return UIApplicationMain(argc, argv, nil,                                  NSStringFromClass([AppDelegate class]));     } } 

And in -[XCTestCase setUp] add:

XCUIApplication *app = [[XCUIApplication alloc] init]; app.launchArguments = @[@"--reset-container"]; [app launch]; 
like image 175
Mats Avatar answered Oct 12 '22 00:10

Mats