2016-11-17 104 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 –

相關問題