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?
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.
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”.
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.
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.
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];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With