[FIXED] Run specific Android instrumentation test with Gradle

Issue

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.

Solution

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

Answered By – Dinesh

Answer Checked By – Laura B. (Easybugfix Admin)

Leave a Reply

(*) Required, Your email will not be published