Android studio Android Studio--清除用于检测测试的应用程序数据
如何让Android Studio(AndroidJunitRunner)在检测测试之前清除应用程序数据,而不必手动运行Android studio Android Studio--清除用于检测测试的应用程序数据,android-studio,gradle,android-gradle-plugin,android-instrumentation,Android Studio,Gradle,Android Gradle Plugin,Android Instrumentation,如何让Android Studio(AndroidJunitRunner)在检测测试之前清除应用程序数据,而不必手动运行adb命令? 我发现,android.support.test.runner.AndroidJUnitRunner是一种欺骗——它实际上从未调用connectedCheck或connectedAndroidTest 从命令行运行时,$gradle connectedCheck :MyMainApp:assembleDebug UP-TO-DATE :MyMainApp:asse
adb
命令?
我发现,android.support.test.runner.AndroidJUnitRunner
是一种欺骗——它实际上从未调用connectedCheck
或connectedAndroidTest
$gradle connectedCheck
:MyMainApp:assembleDebug UP-TO-DATE
:MyMainApp:assembleDebugTest UP-TO-DATE
:MyMainApp:clearMainAppData
:MyMainApp:connectedCheck
assembleDebugTest
build.gradle
中的connectedCheck
上添加了一个钩子,以在开始仪器测试之前清除主应用程序的数据
// 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)
}
}
我意识到,我也可以在主应用程序中实现一个“清除数据”按钮,并让instrumentation应用程序在UI中单击,但我发现这种解决方案并不可取
我查看了AndroidJUnitRunner
API,通过Runlistener
接口有钩子,但钩子在测试应用程序的上下文中,即在设备上运行,Android禁止一个应用程序修改另一个应用程序。
如果您能帮助我从Android Studio中自动触发以下事件之一,您将获得最佳答案:
- 执行命令行
adb shell pm clear my.main.app.package
- 或者最好调用我的gradle任务
clearMainAppData
谢谢大家! 我知道这已经有一段时间了,希望到现在为止你能解决这个问题 我今天遇到了同样的问题,在这里崩溃了,没有任何解决方案 但是我通过从测试配置调用我的任务,成功地使它工作 步骤1:转到测试配置 步骤2:只需添加您创建的gradle任务 顺便说一句,在我的例子中,任务是这样的:
task clearData(type: Exec) {
def clearDataCommand = ['adb', 'shell', 'pm', 'clear', 'com.your.application']
commandLine clearDataCommand
}
希望这将帮助某人:)使用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'
}
下面是Android测试编排器的链接
没有响应,所以我想大多数人只是依靠安装应用程序作为初始化过程。@razzle shazi nsimons解决方案对您有效吗?如果有人有问题:“多个设备和模拟器”,只有一个设备实际运行,请尝试重新启动adb。只需要:“adb kill server”和“adb devices”。可以工作,但任务只启动一次。你知道在每次测试前如何开始吗?如果你看第一个屏幕截图,你会看到底部的框写着“启动前”。确保在那里定义了gradle任务(通过执行步骤2)。现在,每次启动该配置时,gradle任务(此处为clearData)将首先执行,然后执行您的配置。请注意,此解决方案仅在仅连接一个设备时有效如何保存命令行执行的结果,假设我有命令行“pwd”,它返回路径目录,因此,如果您希望清除每个测试的数据,那么如何将其保存到可由另一个任务访问的变量中是最好的方法。但是,当我应用Orchestrator时,我会在下次尝试运行两个缺少的位时立即得到“未找到测试”:
testOptions{animationsDisabled=true执行'ANDROIDX_TEST_ORCHESTRATOR'}
和依赖项{androidTestUtil'ANDROIDX.TEST:ORCHESTRATOR:1.2.0'}
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'
}