Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running a specific instrumentation unit test with Gradle

Is there a way to run a specific Android instrumentation unit test using Gradle? I've tried

gradle -Dtest.single=UnitTestName connectedInstrumentTest

but it seems to run all the tests in the package.

like image 360
Ivan Gromov Avatar asked Oct 24 '13 12:10

Ivan Gromov


People also ask

How do I run a specific test in Gradle?

In the Run test using list, select one of the following test runner options for the selected Gradle project: Gradle: IntelliJ IDEA uses Gradle as a default test runner. As an outcome, you get the same test results on the continuous integration (CI) server.

Which command lets you generate the unit test of a project developed with Gradle?

We can run our unit tests by using the command: gradle clean test.


4 Answers

Using test.single appears to be deprecated. The new correct way to do this is

./gradlew :<module>:test --tests <pattern>

where <pattern> could be something like:

  • com.example.MyTest to run all test methods in com.example.MyTest
  • *MyTest to match every method in every class whose name ends with MyTest
  • *.MyTest.myMethod to run a specific test method in class MyTest in any package

If you have a multi-project build, make sure to give the module path before the test task; otherwise you'll get a misleading error message when it searches for your test pattern in every subproject.

None of this is documented on the Gradle site anywhere I could find it.

like image 169
Chris Jones Avatar answered Nov 01 '22 12:11

Chris Jones


This works if you're using an instrumentationTestRunner:

./gradlew test -Pandroid.testInstrumentationRunnerArguments.class=<pkg>.YourClassName

Using gradle 2.10 and android gradle plugin 2.0.0-beta2.

Since you know what test(s) you want to run, you probably know which module / flavor to use too. You can help Gradle out by specifying the exact module and Gradle task. So if your test is in the app module and you want to test the debug flavor:

./gradlew app:connectedDebugAndroidTest -Pandroid.testInstrumentationRunnerArguments.class=<pkg>.YourClassName

You can get even more fancy with the tests_regex argument instead:

./gradlew app:connectedDebugAndroidTest -Pandroid.testInstrumentationRunnerArguments.tests_regex=PartialClassName*
./gradlew app:connectedDebugAndroidTest -Pandroid.testInstrumentationRunnerArguments.tests_regex=partialMethodName*
like image 29
jlhonora Avatar answered Nov 01 '22 13:11

jlhonora


The pattern is -D<testTaskName>.single=<TestClass> so in your example it should be:

gradle -DconnectedInstrumentTest.single=UnitTestName connectedInstrumentTest

NOTE: This answer is outdated. You should use the --tests switch in the latest versions of Gradle. (see other answers for an explanation)

like image 21
erdi Avatar answered Nov 01 '22 12:11

erdi


Since Android gradle plugin 1.1.0-rc1, one can run single test class using --tests flag by executing:

./gradlew app:testDebug --tests=com.example.MyTest

See http://tools.android.com/tech-docs/unit-testing-support#TOC-Running-from-Gradle

like image 7
hidro Avatar answered Nov 01 '22 13:11

hidro