Android Studio -- 为 Instrumentation Test 清除应用数据 [英] Android Studio -- clear application data for Instrumentation Test
问题描述
如何在不手动运行 adb
命令的情况下让 Android Studio (AndroidJunitRunner) 在仪器测试之前清除应用程序数据?
我发现 android.support.test.runner.AndroidJUnitRunner
是一种作弊——它实际上从未调用 connectedCheck
或 connectedAndroidTest
.
从命令行运行时
$ gradle connectedCheck
:MyMainApp:assembleDebug UP-TO-DATE:MyMainApp:assembleDebugTest 最新:MyMainApp:clearMainAppData:MyMainApp:connectedCheck
在 IDE 中通过单击仪器测试配置(带有红色/绿色箭头的绿色 Android 机器人徽标)运行时
**执行任务:[:MyMainAppApp:assembleDebug, :MyMainAppApp:assembleDebugTest]**
如你所见,最后一个gradle目标是
assembleDebugTest
我在 build.gradle
中的 connectedCheck
上添加了一个钩子,以便在开始仪器测试之前清除主应用程序的数据.
//运行'adb' shell 命令来清除'debug' 变体的主应用程序的应用程序数据任务 clearMainAppData(类型:Exec){//我们必须迭代以找到调试"变体以获得变体引用android.applicationVariants.all { 变体 ->如果(variant.name.equals(调试")){def clearDataCommand = ['adb', 'shell', 'pm', 'clear', getPackageName(variant)]println "正在清除 ${variant.name} 变体的应用数据:[${clearDataCommand}]"命令行 clearDataCommand}}}//在运行仪器测试之前清除应用程序数据(一次)tasks.whenTaskAdded { 任务 ->//这两个目标今天是等价的,尽管在未来 connectedCheck//还将包括 connectedUiAutomatorTest(尚未实现)if(task.name.equals("connectedAndroidTest") || task.name.equals("connectedCheck" )){task.dependsOn(clearMainAppData)}}
我意识到,我也可以在主应用程序中实现清除数据"按钮并让仪表应用程序通过 UI 单击,但我发现该解决方案不可取.
我查看了 AndroidJUnitRunner
API 并且有通过 Runlistener
接口的钩子,但钩子是在测试应用程序的上下文中,即在设备上运行,而 Android 禁止一个应用程序从修改另一个应用程序.
第 2 步:只需添加您创建的 gradle 任务
顺便说一下,我的任务看起来像这样:
task clearData(type: Exec) {def clearDataCommand = ['adb', 'shell', 'pm', 'clear', 'com.your.application']命令行 clearDataCommand}
希望这会对某人有所帮助:)
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
.
When run from command line
$ gradle connectedCheck
:MyMainApp:assembleDebug UP-TO-DATE :MyMainApp:assembleDebugTest UP-TO-DATE :MyMainApp:clearMainAppData :MyMainApp:connectedCheck
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!
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 :)
这篇关于Android Studio -- 为 Instrumentation Test 清除应用数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!