2

I wrote tests in Android Studio using espresso. Now there are a number of tests that before I run I have to delete the app's cache. I tried a lot of options that I know but nothing worked out. I searched the site for the problem and tried the results but none of them worked either.

For example, there is a stage in the app that causes a change gender addressing(My app is in foreign language) and I test a number of things in this section, I log in from 3 different test users and each one has a different view that cann't change unless cache is deleted and without deleting the cache I cann't run them together but I can run each one of them separately. The app defines itself in the momnet the user logs in so to switch users I need to delete the app cache.

I attached some links here of what I tried and should have worked but didn't. They may be able to help and explain

Clear database before testcase espresso

Reset app state between InstrumentationTestCase runs

https://github.com/chiuki/espresso-samples/issues/3

https://discuss.appium.io/t/android-how-to-clear-app-data-before-test/7166/10

Gal El
  • 33
  • 1
  • 4

3 Answers3

3

Clearing the database once per test class

Add the following code to your Android Test class:

companion object {
    @BeforeClass
    fun clearDatabase() {
        InstrumentationRegistry.getInstrumentation().uiAutomation.executeShellCommand("pm clear PACKAGE_NAME").close()
    }
}

Clearing the database before every test

An alternative way to have the database cleared before each test run is to set the clearPackageData flag while using Android Test Orchestrator. This will "remove all shared state from your device's CPU and memory after each test:"

Add the following statements to your project's build.gradle file:

android {
  defaultConfig {
   ...
   testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

   // The following argument makes the Android Test Orchestrator run its
   // "pm clear" command after each test invocation. This command ensures
   // that the app's state is completely cleared between tests.
   testInstrumentationRunnerArguments clearPackageData: 'true'
 }

  testOptions {
    execution 'ANDROIDX_TEST_ORCHESTRATOR'
  }
}

dependencies {
  androidTestImplementation 'androidx.test:runner:1.1.0'
  androidTestUtil 'androidx.test:orchestrator:1.1.0'
}
Thomas Gales
  • 107
  • 2
  • 8
  • Your first code block doesn't seem to work because my app doesn't boot after it is run and neither do subsequent tests. Its almost like its deleting the app instead? – George Nov 02 '21 at 17:44
1

For me, I added BuildConfig to find the package for you (in chance you change it later)

  @Before
    fun setup() {
        InstrumentationRegistry.getInstrumentation().uiAutomation.executeShellCommand("pm clear ${BuildConfig.APPLICATION_ID}").close()
    }

The second way of doing it is easily adding a single line into the build gradle

android {
 defaultConfig {
  ...
  testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

  // The following argument makes the Android Test Orchestrator run its
  // "pm clear" command after each test invocation. This command ensures
  // that the app's state is completely cleared between tests.
  testInstrumentationRunnerArguments clearPackageData: 'true'
 }

 testOptions {
  execution 'ANDROIDX_TEST_ORCHESTRATOR'
 }
}

dependencies {
 androidTestImplementation 'androidx.test:runner:1.1.0'
 androidTestUtil 'androidx.test:orchestrator:1.1.0'
}

Refers to: https://developer.android.com/training/testing/instrumented-tests/androidx-test-libraries/runner#enable-gradle

Gastón Saillén
  • 12,319
  • 5
  • 67
  • 77
-2

Kotlin

@Test
fun clearStorage() {
    InstrumentationRegistry.getInstrumentation().uiAutomation.executeShellCommand("pm clear PACKAGE NAME").close()
}
Mihe
  • 2,270
  • 2
  • 4
  • 14