2016-11-17 108 views
1

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

Android Studio通过复杂的adb shell am instrument提供此功能。

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

我已经阅读了关于How to run only one test class on gradle,Test from the Command Line,但它是相当复杂的做法。

这是Android的工作室是如何启动的具体测试类:

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. 
+0

也许只是将这些adb命令包装到自定义gradle任务中。 – WenChao

+0

检查这个问题 https://stackoverflow.com/questions/21288448/how-to-retrieve-path-to-adb-in-build-gradle –

回答

1

你可以写一个gradle这个任务是这样的:

在主gradle这个文件

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

然后创建一个目录工具和文件script-integration-tests.gradle在项目目录中。然后用这样的测试类名填写它。

apply plugin: 'com.android.application' 

task intergrationTest(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' 
} 
+0

然后运行集成测试作为一个gradle任务./gradlew intergrationTest –

相关问题