Ensuring Reliable UI Tests by Resetting Application Data in Xcode

Ensuring Reliable UI Tests by Resetting Application Data in Xcode

11 April 2024 Stephan Petzl Leave a comment Tech-Help

When conducting UI Testing with Xcode, it’s imperative to start with a clean state for each test to ensure that previous test data does not affect the outcomes. This article provides a guide on how to reset the application data before each test suite runs, thus enabling more reliable and independent tests.

Method 1: Utilize Command Line Arguments

One approach to resetting your application data is to use command line arguments that can be recognized by your application during the testing phase. This requires a slight modification to your application’s main function and the test setup.

Updating the main.m File

In your main.m file, you can add the following code snippet within the DEBUG preprocessor macro. This code checks for a specific command line argument and deletes data in the Library and Documents directories accordingly.

int main(int argc, char * argv[]) {
    // 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...
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil,
                                 NSStringFromClass([AppDelegate class]));

Adjusting the XCTestCase setUp Method

In the setUp method of your test class, you can specify the launch arguments for your application as follows:

XCUIApplication *app = [[XCUIApplication alloc] init];
app.launchArguments = @[@"--reset-container"];
[app launch];

Method 2: Using Application Launch Environment

Another way to prepare your app for UI Testing is by setting up an environment variable during the application launch process.

Modifying the setUp Method in XCTestCase

In your XCTestCase’s setUp method, you can set a specific environment variable that the application will check upon launch:

let application = XCUIApplication()
application.launchEnvironment = ["UITESTS":"1"]

Checking the Environment Variable

Within your application’s didFinishLaunchingWithOptions method, you can check for the “UITESTS” flag and perform any necessary reset operations:

func application(_ application: UIApplication,
                 didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {

    let env = ProcessInfo.processInfo.environment
    if let uiTests = env["UITESTS"], uiTests == "1" {
        // Perform reset operations
    // Further set up code

Remember to encapsulate this testing code within a DEBUG preprocessor macro to ensure it does not affect your production build.


Employing either of these strategies allows you to reset the application data before each test, making your UI tests more robust and independent. By incorporating these methods into your testing setup, you can ensure that each test starts with a fresh state, improving the consistency and reliability of your test results.

Like this article? there’s more where that came from!