Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear app data using Espresso test programatically?

We are currently writing Espresso UI tests for an area of our application which requires us to clear the app data after each test

  • We have tried previously using Android Orchestrator
  • We have previously tried using the Android test orchestrator with the clearPackageData=true flag, however; this doesn’t play well with Bitrise and local test executions. For example some of us are unable to run tests locally with this flag in place, despite us invalidating the caches etc

-Our next alternative is to try and use an ADB command to clear the package data, but when we use this we are being presented with a process crashed error:

Test failed to run to completion. Reason: 'Instrumentation run failed due to 'Process crashed.''. Check device logcat for details
Test running failed: Instrumentation run failed due to 'Process crashed.'

We've tried to execute the following at the following levels:

Before the class runs Before each test After each test And we are presented with the same error each time. This is the method which we are using.

  public static void clearAppData(){
        try {
            InstrumentationRegistry.getInstrumentation().getUiAutomation().executeShellCommand("pm clear <packageName>");
        }
        catch (Exception e){
            e.printStackTrace();
        }
    }

Option: Can we do the database reset to the dB in the App itself?

like image 609
user2451016 Avatar asked Oct 27 '25 04:10

user2451016


1 Answers

It will likely be easiest to have the development team enable a DB reset function inside the app when running in a debug version. Ideally it’d be a button on the initial screen so you don’t have to do much work to get to it, unless your app returns to the previous state between tests (I don’t have a functioning environment in front of me to check) in which case that code is going to get messy.

You’re correct that the methods you’ve mentioned in your question will not work; abandon them.

like image 125
Mike Collins Avatar answered Oct 29 '25 06:10

Mike Collins