简体   繁体   English

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

[英]Run specific Android instrumentation test with Gradle

I need to execute certain Android test case (Class or method) with gradle from console.我需要从控制台使用 gradle 执行某些 Android 测试用例(类或方法)。 It is located at src/androidTest/java/com/example/CertainTest.java .它位于src/androidTest/java/com/example/CertainTest.java

Android Studio makes this available via complicated adb shell am instrument . Android Studio 通过复杂的adb shell am instrument实现了这一点。

Is it possible to invoke certain Android Test only via Gradle?是否可以仅通过 Gradle 调用某些 Android 测试?

I have read about How to run only one test class on gradle , Test from the Command Line but is it quite complicated way of doing that.我已经阅读了关于How to run only one test class on gradle , Test from the Command Line但这样做的方法是否相当复杂。

This is how Android Studio launches specific Test Class:这是 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.

You can write a gradle task like this:您可以编写这样的 gradle 任务:

In the main gradle file在主gradle文件中

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

Then create a directory tools and a file script-integration-tests.gradle in the project directory.然后在项目目录下创建目录tools和文件script-integration-tests.gradle。 Then fill it with the test class name like this.然后像这样用测试类名填充它。

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.

相关问题 在(Espresso)Android仪器测试中启动特定的导体控制器 - Start specific conductor controller in (Espresso) Android instrumentation test 如何通过gradle在特定文件夹中运行jUnit测试? - How to run jUnit test in specific folder by gradle? 在Android检测测试中模拟资源 - Mocking a resource on Android instrumentation test Android Espresso UI测试-测试运行失败:由于“ java.lang.IllegalAccessError”,检测运行失败 - Android Espresso UI test - Test running failed: Instrumentation run failed due to 'java.lang.IllegalAccessError' 仪表测试将作为本地单元测试在Android Studio 3(空测试套件)上运行 - Instrumentation Tests will run as Local Unit tests on Android Studio 3 (Empty test suite) 获取android gradle junit测试运行的XML / HTML测试结果 - Getting XML / HTML test results for android gradle junit test run Android中的多点触控测试 - Multi-touch instrumentation test in android 获取Android Instrumentation Test上的当前活动 - Get current Activity on Android Instrumentation Test JUNIT仪表测试在Android Studio中失败 - JUNIT instrumentation test failing in android studio gradle测试任务如何运行特定的junit测试套件? - how can gradle test task run specific junit tests' suite?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM