Android Studio - 清除Instrumentation Test的应用程序数据

Raz*_*azl 13 gradle android-studio android-gradle-plugin android-instrumentation

如何在不手动运行adb命令的情况下让Android Studio(AndroidJunitRunner)清除检测测试之前的应用程序数据?

我发现了android.support.test.runner.AndroidJUnitRunner一种作弊的-它从来没有真正调用connectedCheckconnectedAndroidTest.

  1. 从命令行运行时 $ gradle connectedCheck

    :MyMainApp:assembleDebug UP-TO-DATE
    :MyMainApp:assembleDebugTest UP-TO-DATE
    :MyMainApp:clearMainAppData
    :MyMainApp:connectedCheck
    
    Run Code Online (Sandbox Code Playgroud)
  2. 通过单击检测测试配置从IDE内部运行(带有红色/绿色箭头的绿色Android机器人徽标)

    **Executing tasks: [:MyMainAppApp:assembleDebug, :MyMainAppApp:assembleDebugTest]**
    
    Run Code Online (Sandbox Code Playgroud)

    如您所见,最后一个gradle目标是 assembleDebugTest

我已经添加了一个钩到connectedCheckbuild.gradle开始仪表测试之前清除主要的应用程序的数据.

// 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)
    }
}
Run Code Online (Sandbox Code Playgroud)

我意识到,或者我可以在主应用程序中实现"清除数据"按钮,并让检测应用程序单击UI,但我发现该解决方案不合适.

我查看了AndroidJUnitRunnerAPI并且通过Runlistener接口有钩子但是钩子是在测试应用程序的上下文中,即在设备上运行,而Android禁止一个应用程序修改另一个应用程序. http://junit.sourceforge.net/javadoc/org/junit/runner/notification/RunListener.html

如果您可以帮助我在Android Studio中自动触发以下某项操作,那么最佳答案就是您:

  • 执行命令行adb shell pm clear my.main.app.package,
  • 或者最好调用我的gradle任务 clearMainAppData

如果有另一种方式,我也会全力以赴.当然,设备测试自动化应该有一个明确的方法来清除应用程序数据?

谢谢!

NSi*_*mon 25

我知道它已经有一段时间了,希望到现在为止你会解决这个问题.

我今天遇到了同样的问题,并在没有任何解决方案的情况下崩溃了.

但我设法通过从测试配置中调用我的任务来使其工作.

第1步:转到您的测试配置

您的测试配置

第2步:只需添加您创建的gradle任务

只需从这里调用您的gradle任务

顺便说一句,在我的情况下,任务看起来像这样:

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

希望这会帮助别人:)


Pon*_*rai 17

使用 Android Test Orchestrator,可以更轻松地通过 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'
 }
Run Code Online (Sandbox Code Playgroud)

以下是 Android Test Orchestrator 的链接

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

  • 缺少两个位:`testOptions {animationsDisabled = trueexecution'ANDROIDX_TEST_ORCHESTRATOR'}`和`dependences { androidTestUtil'androidx.test:orchestrator:1.2.0'}` (8认同)
  • 但是当我应用 Orchestrator 时,下次尝试运行时我会立即收到“未找到测试”的信息 (2认同)