22

How can I get Android Studio (AndroidJunitRunner) to clear application data preceding an instrumentation test without manually running adb command?

I discovered that android.support.test.runner.AndroidJUnitRunner kind of cheats -- it never actually invokes connectedCheck or connectedAndroidTest.

  1. When run from command line $ gradle connectedCheck

    :MyMainApp:assembleDebug UP-TO-DATE
    :MyMainApp:assembleDebugTest UP-TO-DATE
    :MyMainApp:clearMainAppData
    :MyMainApp:connectedCheck
    
  2. When run from within IDE by clicking the instrumentation test configuration (green Android robot logo with red/green arrows)

    **Executing tasks: [:MyMainAppApp:assembleDebug, :MyMainAppApp:assembleDebugTest]**
    

    As you can see, the last gradle target is assembleDebugTest

I had added a hook onto connectedCheck in build.gradle to clear the data of the main app before starting the instrumentation test.

// Run 'adb' shell command to clear application data of main app for 'debug' variant
task clearMainAppData(type: Exec) {
    // we have to iterate to find the 'debug' variant to obtain a variant reference
    android.applicationVariants.all { variant ->
        if (variant.name.equals("debug")) {
            def clearDataCommand = ['adb', 'shell', 'pm', 'clear', getPackageName(variant)]
            println "Clearing application data of ${variant.name} variant: [${clearDataCommand}]"
            commandLine clearDataCommand
        }
    }
}
// Clear Application Data (once) before running instrumentation test
tasks.whenTaskAdded { task ->
    // Both of these targets are equivalent today, although in future connectedCheck
    // will also include connectedUiAutomatorTest (not implemented yet)
    if(task.name.equals("connectedAndroidTest") || task.name.equals("connectedCheck" )){
        task.dependsOn(clearMainAppData)
    }
}

I realize that alternatively I could implement a 'clear data' button in the main app and have the instrumentation app click through the UI, but I find that solution undesirable.

I looked at AndroidJUnitRunner API and there are hooks via Runlistener interface but hooks are during the context of the test app, i.e. running on device, and Android forbids one app from modifying another app. http://junit.sourceforge.net/javadoc/org/junit/runner/notification/RunListener.html

Best answer goes to you if you can help me trigger one of the following automatically from within Android Studio:

  • execute a command line adb shell pm clear my.main.app.package,
  • or preferably invoke my gradle task clearMainAppData

I'm also all ears if there is an alternate way. Surely with device testing automation there should be a clear way to clear application data?

Thank you!

Razzle Shazl
  • 1,287
  • 1
  • 8
  • 20

3 Answers3

32

I know it's been a while, and hopefully by now you will have this issue sorted.

I ran into that same issue today, and crashed here without any solution.

But I managed to make it work by calling my task from the test configuration.

Step 1 : Go to your test configuration

Your test configuration

Step 2 : Simply add the gradle task you created

Simply call your gradle task from here

By the way, the task in my case simply looks like this :

task clearData(type: Exec) {
  def clearDataCommand = ['adb', 'shell', 'pm', 'clear', 'com.your.application']
  commandLine clearDataCommand
}

Hope this will help someone :)

NSimon
  • 5,212
  • 2
  • 22
  • 36
  • If someone has a problem: "more than one device and emulator" and only one actually running device then try restart your adb. Just need: "adb kill-server" and then "adb devices". – ZelvaJan May 31 '16 at 13:23
  • 1
    Works but the task is started only once. Do you know some way to start before each test? – WindRider Sep 14 '16 at 15:51
  • If you look at the first screenshot, you'll see that the very bottom box that says "Before launch". Make sure you have that gradle task defined there (by following step2). Now each time you launch that configuration, the gradle task (here clearData) will be executed first, and then your configuration. Note that this solution only works when you only have one device connected – NSimon Sep 16 '16 at 08:27
  • How to save the result of the command line execution, suppose I have commandLine 'pwd' it returns the path directory, so how to save it to a variable that it can be accessed by another task too – Srinivas Jayaram Jun 15 '18 at 12:10
  • WindRider, when you're running these instrumented tests, they're running on the emulator/device. Anything that happens between tests is going to need to be part of your test code/app. – Scott Merritt Feb 22 '23 at 18:57
26

With Android Test Orchestrator it is easier to provide this option via gradle script.

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'
 }

Below is the link for Android Test Orchestrator

https://developer.android.com/training/testing/junit-runner#using-android-test-orchestrator

Ponsuyambu
  • 7,876
  • 5
  • 27
  • 41
  • 1
    This is the best way to do it if you want the data cleared for each test. – Matt Wolfe Oct 25 '19 at 16:35
  • 2
    But when I apply Orchestrator, I instantly get "No tests found" the next time I try to run – Emjey Jan 05 '20 at 16:07
  • 8
    Two missing bits: `testOptions { animationsDisabled = true execution 'ANDROIDX_TEST_ORCHESTRATOR' }` and `dependences { androidTestUtil 'androidx.test:orchestrator:1.2.0' }` – Ivan Morgillo May 08 '20 at 09:24
0

Solution in 2023

  1. Add dependencies (check actual versions here) :

With Gradle:

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

With version catalog:

[versions]
testRunner = "1.5.2"
orchestrator = "1.4.2"

[libraries]
orchestrator = { group = "androidx.test", name = "runner", version.ref = "testRunner" }
orchestrator = { group = "androidx.test", name = "orchestrator", version.ref = "orchestrator" }

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

Kotlin:

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 += mapOf(
            "clearPackageData" to "true",
        )
}

testOptions {
    execution = "ANDROIDX_TEST_ORCHESTRATOR"
}

Groovy:

defaultConfig {
  testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
  testInstrumentationRunnerArguments clearPackageData: 'true'
}

testOptions {
  execution 'ANDROIDX_TEST_ORCHESTRATOR'
}

Android Developers docs

tasjapr
  • 632
  • 4
  • 13