Search code examples
androidandroid-espressoandroid-uiautomatorui-testing

How to clean application data with 'pm clear' after each UI test


I started writing UI tests in android using Espresso Framework and UI Automator.

So I wrote 3 different UI tests, all of them containing the authentication process. When I run each test individually they are working just fine. But, when I run all 3 tests using ./gradlew connectedAndroidTest, the first test passes and the other 2 tests fail, because it auto logins (I use webview for login so it redirects me to main screen). I tried this solution but it didn't work.

What I want to archive is, after each test, to clear the data using InstrumentationRegistry.getInstrumentation().getUiAutomation().executeShellCommand("pm clean com.example.app") and have a fresh start for each test.

when I execute this in @After, the tests crashes:

Test failed to run to completion. Reason: 'Instrumentation run failed due to 'Process crashed.''.

Any Idea how I can archive this?


Solution

  • I would advise you to use Android Test Orchestrator, since it will run each test in a separate process, which means that no state or data is shared with other tests.