运行 使用 Gradle 的特定 Android 仪器测试

Run specific Android instrumentation test with Gradle

我需要从控制台使用 gradle 执行某些 Android 测试用例(Class 或方法)。 它位于 src/androidTest/java/com/example/CertainTest.java.

Android Studio 通过复杂的 adb shell am instrument.

使其可用

是否可以仅通过 Gradle 调用某些 Android 测试?

我读过 How to run only one test class on gradle, Test from the Command Line 但这样做是不是很复杂。

这就是 Android Studio 启动特定测试的方式 Class:

Launching Tests
$ adb push /.../app/build/outputs/apk/app-debug.apk /data/local/tmp/com.example.andrii.espressotutorial
$ adb shell pm install -r "/data/local/tmp/com.example.andrii.espressotutorial"
    pkg: /data/local/tmp/com.example.andrii.espressotutorial
Success
$ adb push /.../app/build/outputs/apk/app-debug-androidTest.apk /data/local/tmp/com.example.andrii.espressotutorial.test
$ adb shell pm install -r "/data/local/tmp/com.example.andrii.espressotutorial.test"
    pkg: /data/local/tmp/com.example.andrii.espressotutorial.test
Success
Running tests
$ adb shell am instrument -w -r   -e debug false -e class com.example.andrii.espressotutorial.ExampleInstrumentedTest2 com.example.andrii.espressotutorial.test/android.support.test.runner.AndroidJUnitRunner
Client not ready yet..
Started running tests
Tests ran to completion.

您可以像这样编写 gradle 任务:

在主 gradle 文件中

apply from: "$project.rootDir/tools/script-integration-tests.gradle"

然后在项目目录下新建tools目录和script-integration-tests.gradle文件。然后用这样的测试 class 名称填充它。

apply plugin: 'com.android.application'

task integrationTest(type: Exec) {
    def adb = android.getAdbExe().toString()
    commandLine "$adb", 'shell', 'am', 'instrument', '-w', '-r', '-e',
            'debug', 'false', '-e', 'class', 'de.app.id.PullRequestTests',
            'de.app.id.app_test.debug.test/android.support.test.runner.AndroidJUnitRunner'
}