Running a Specific Instrumentation Unit Test with Gradle

How can I run a single instrumentation test with Gradle Android

you can run the single android test in two steps:

  1. ./gradlew installDebugAndroidTest
  2. adb shell am instrument -w -e class com.example.MyInstrumentationTest#testFoo com.example.test/android.support.test.runner.AndroidJUnitRunner

    https://developer.android.com/tools/testing/testing_otheride.html

Run specific Android instrumentation test with Gradle

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

How to run only one unit test class using Gradle

To run a single test class Airborn's answer is good.

With using some command line options, which found here, you can simply do something like this.

gradle test --tests org.gradle.SomeTest.someSpecificFeature
gradle test --tests '*SomeTest.someSpecificFeature'
gradle test --tests '*SomeSpecificTest'
gradle test --tests 'all.in.specific.package*'
gradle test --tests '*IntegTest'
gradle test --tests '*IntegTest*ui*'
gradle test --tests '*IntegTest.singleMethod'
gradle someTestTask --tests '*UiTest' someOtherTestTask --tests '*WebTest*ui'

From version 1.10 of gradle it supports selecting tests, using a test filter. For example,

apply plugin: 'java'

test {
filter {
//specific test method
includeTestsMatching "org.gradle.SomeTest.someSpecificFeature"

//specific test method, use wildcard for packages
includeTestsMatching "*SomeTest.someSpecificFeature"

//specific test class
includeTestsMatching "org.gradle.SomeTest"

//specific test class, wildcard for packages
includeTestsMatching "*.SomeTest"

//all classes in package, recursively
includeTestsMatching "com.gradle.tooling.*"

//all integration tests, by naming convention
includeTestsMatching "*IntegTest"

//only ui tests from integration tests, by some naming convention
includeTestsMatching "*IntegTest*ui"
}
}

For multi-flavor environments (a common use-case for Android), check this answer, as the --tests argument will be unsupported and you'll get an error.

Android, Gradle. How start specific instrumented test method?

You can run all tests in a specific test class like this

gradlew connectedDevAndroidTest -Pandroid.testInstrumentationRunnerArguments.class=com.example.StringUtilTest

You can run a single test in a specific test class like this

gradlew connectedDevAndroidTest -Pandroid.testInstrumentationRunnerArguments.class=com.example.StringUtilTest#testSelectLanguageFragment

More info at https://developer.android.com/studio/test/command-line.html

Gradle: How to run instrumentation test for class

As stated in the AndroidTestingBlueprint you can use the android.testInstrumentationRunnerArguments.class property:

./gradlew app:connectedAndroidTest -Pandroid.testInstrumentationRunnerArguments.class=com.example.android.testing.blueprint.ui.espresso.EspressoTest

Is there a way to only run a specific set of instrumentation tests in an Android Gradle project?

Since Android Gradle Plugin 1.3.0

Starting from version 1.3.0 you can (finally!) specify the arguments the Android Gradle Plugin have to pass to the InstrumentationTestRunner.

For example, if you want to run only the tests annotated with @SmallTest and ignore the others:

android {
//....
defaultConfig {
//....
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
testInstrumentationRunnerArgument "size", "small"
}
}

Old workaround
Prior to plugin 1.3.0 is not possible to do that but I've found a little workaound. Basically I've annotated with the @SmallTest annotation the fast tests and using a custom subclass of the InstrumentationTestRunner I'm able to run just them and not the whole suite.

You can found the example code in this gist.

Can Gradle build Instrumentation tests without running them?

Found the Gradle task to achieve this:

$ ./gradlew compileDebugAndroidTestJavaWithJavac

Above task will compile Instrumented tests without actually running them.



Related Topics



Leave a reply



Submit