Android Studio — clear application data for Instrumentation Test

前端 未结 2 1276
失恋的感觉
失恋的感觉 2020-12-03 17:06

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

相关标签:
2条回答
  • 2020-12-03 17:43

    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

    Step 2 : Simply add the gradle task you created

    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 :)

    0 讨论(0)
  • 2020-12-03 18:01

    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

    0 讨论(0)
提交回复
热议问题