繁体   English   中英

使用 Gradle 运行特定的 Android 检测测试

[英]Run specific Android instrumentation test with Gradle

我需要从控制台使用 gradle 执行某些 Android 测试用例(类或方法)。 它位于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 启动特定测试类的方式:

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。 然后像这样用测试类名填充它。

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'
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM