Andrii Abramov
Andrii Abramov

Reputation: 10751

Run specific Android instrumentation test with Gradle

I need to execute certain Android test case (Class or method) with gradle from console. It is located at src/androidTest/java/com/example/CertainTest.java.

Android Studio makes this available via complicated adb shell am instrument.

Is it possible to invoke certain Android Test only via Gradle?

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.

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

Upvotes: 1

Views: 1794

Answers (1)

DVG
DVG

Reputation: 898

You can write a gradle task like this:

In the main gradle file

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

Upvotes: 3

Related Questions