Gradle:如何 运行 对 class 进行仪器测试

Gradle: How to run instrumentation test for class

我在 Android Studio 中进行 运行 仪器测试,运行 配置定义如下(不要介意警告):

所以这是为特定 class 调用测试服。我想如何使用命令行实现此目的,我想使用 ./gradlew 命令?

AndroidTestingBlueprint 中所述,您可以使用 android.testInstrumentationRunnerArguments.class 属性:

./gradlew app:connectedAndroidTest -Pandroid.testInstrumentationRunnerArguments.class=com.example.android.testing.blueprint.ui.espresso.EspressoTest

根据the docs

When you run tests from the command-line with Android Debug Bridge (adb), you get more options for choosing the tests to run than with any other method. You can select individual test methods, filter tests according to their annotation, or specify testing options. Since the test run is controlled entirely from a command-line, you can customize your testing with shell scripts in various ways.

为了 运行 使用 adb 对特定 class 进行仪器测试,请执行以下操作:

adb shell am instrument -w -e class 'com.myapp.MyActivityTest' com.myapp.test/android.support.test.runner.AndroidJUnitRunner

请注意,如果您在 app/build.gradle 文件中定义了自定义 testInstrumentationRunner,则需要将 android.support.test.runner.AndroidJUnitRunner 替换为您自己的,如下所示:

adb shell am instrument -w -e class 'com.myapp.MyActivityTest' com.myapp.test/com.myapp.MyCustomTestRunner

提示: 如果您因为命令不正确而收到错误,请知道您可以通过 运行 从内部执行测试来简单地获得正确的命令Android工作室。您将在 运行 window 输出中看到该命令。


这 2 个文档页面包含执行选项:

https://developer.android.com/reference/android/support/test/runner/AndroidJUnitRunner#typical-usage

https://developer.android.com/studio/test/command-line#AMSyntax