How to run only one unit test class using Gradle?
Asked Answered
I

12

664

I am new to Gradle. I use Gradle 1.10 and Ubuntu 13.

I want to know if there's any command to execute only one unit test class, similar to testOnly in SBT.

Intransigence answered 19/3, 2014 at 12:15 Comment(2)
Given the answers, it should be clear this is about local tests, not instrumented tests. Android / Gradle makes things confusing by calling these local tests "unit" tests.Combustion
Switched wording from "local test" to "unit test", since "local test" seems to be an Android-specific concept, but this question is much more general than that.Lamont
O
937

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.

Outfit answered 17/7, 2015 at 5:26 Comment(4)
the filter approach could likely also be combined with Task rules for some convenient test selectionHeda
For large test suites, even if using --tests to call a single test method within the class, then we see iteration over all classes even all those not being run, which is too slow. Don't know if that slowness is due to our env or if it would impact others. Can update if finding more.Milda
if you need to test a nested class gradle test --tests org.gradle.SomeTest$NestedClassTestOversubscribe
The singleMethod one doesn't work for me (anymore?).Theater
L
220

In versions of Gradle prior to 5, the test.single system property can be used to specify a single test.

You can do gradle -Dtest.single=ClassUnderTestTest test if you want to test single class or use regexp like gradle -Dtest.single=ClassName*Test test you can find more examples of filtering classes for tests under this link.

Gradle 5 removed this option, as it was superseded by test filtering using the --tests command line option.

Lalonde answered 19/3, 2014 at 13:2 Comment(0)
T
175

In case you have a multi-module project :

let us say your module structure is

root-module
 -> a-module
 -> b-module

and the test(testToRun) you are looking to run is in b-module, with full path : com.xyz.b.module.TestClass.testToRun

As here you are interested to run the test in b-module, so you should see the tasks available for b-module.

./gradlew :b-module:tasks

The above command will list all tasks in b-module with description. And in ideal case, you will have a task named test to run the unit tests in that module.

./gradlew :b-module:test

Now, you have reached the point for running all the tests in b-module, finally you can pass a parameter to the above task to run tests which matches the certain path pattern

./gradlew :b-module:test --tests "com.xyz.b.module.TestClass.testToRun"

Now, instead of this if you run

./gradlew test --tests "com.xyz.b.module.TestClass.testToRun"

It will run the test task for both module a and b, which might result in failure as there is nothing matching the above pattern in a-module.

Thorvald answered 11/6, 2016 at 10:45 Comment(2)
Or simply ./gradlew :b-module:test --tests testToRunSelfsustaining
Is there a way to determine the module in question by its underlying test? I'd like to rerun tests that only fail on our Jenkins. But I also don't get the module information from Jenkins.Pristine
U
64

Please note that --tests option may not work if you have different build types/flavors (fails with Unknown command-line option '--tests'). In this case, it's necessary to specify the particular test task (e.g. testProdReleaseUnitTest instead of just test)

Underplay answered 1/8, 2018 at 9:19 Comment(3)
Can you elaborate a full example? I'm trying to run 3 tests that match a wildcard using ./gradlew test --tests *testMyCollectionTake* and I can't tell how should I replace test with the wildcarded test nameLach
Also note that the test task has to be used specifically; the check task cannot be used, for instance.Lamont
@Lach try ./gradlew testDebug --tests "*testMyCollectionTake*"Viscometer
F
53

After much figuring out, the following worked for me:

gradle test --tests "a.b.c.MyTestFile.mySingleTest"

Frater answered 11/5, 2018 at 22:30 Comment(3)
this command is not working for me. > Error: Unknown command-line option '--tests'Hippolytus
This is awesome. Easy and convenient to execute specific testcase fileConvex
@Hippolytus - I think the --tests has to follow immediately after 'test'. I got the same...Euphonize
W
19

For multi modules projects it's necessary to use module's name and buildType:

./gradlew :module_name:testDebugUnitTest --tests com.package_name.TestsClass.*

To run some test method the same command, but with test's name:

./gradlew :module_name:testDebugUnitTest --tests com.package_name.TestsClass.test 
Wassyngton answered 19/8, 2021 at 10:45 Comment(1)
Strange, the method name used to work but doesn't anymore. I moved from Gradle 4.8.1 to 6.9.Theater
M
8

Below is the command to run a single test class using gradlew command line option:

gradlew.bat Connected**your bundleVariant**AndroidTest -Pandroid.testInstrumentationRunnerArguments.class=com.example.TestClass

Below example to run class com.example.TestClass with variant Variant_1:

gradlew.bat ConnectedVariant_1AndroidTest -Pandroid.testInstrumentationRunnerArguments.class=com.example.TestClass 
Metallize answered 5/12, 2018 at 13:36 Comment(0)
C
8

Run a single test called MyTest:

./gradlew app:testDebug --tests=com.example.MyTest
Chlortetracycline answered 25/7, 2019 at 11:10 Comment(0)
D
2

You should try to add asteriks (*) to the end.

gradle test --tests "com.a.b.c.*"

Dismuke answered 5/7, 2019 at 18:11 Comment(0)
B
0

In my case, my eclipse java compiler warnings were set too high, and eclipse was not recognizing my class as valid for execution. Updating my compiler settings fixed the problem. You can read more about it here: annotation-nonnull-cannot-be-resolved

Burkhardt answered 18/12, 2018 at 17:51 Comment(0)
R
0

This worked for me

  • Release case:

    gradle testReleaseUnitTest --tests testClass

  • Debug case:

    gradle testDebugUnitTest --tests AsyncExecutorTest

You can see de projects with: gradle -q projects and move to the project where is the class to test

Resurgent answered 31/5, 2021 at 20:59 Comment(0)
M
0

This is for Kotlin DSL (build.gradle.kts) tested with Gradle 8.2:

tasks.test {
    filter {
        includeTestsMatching("*Util*")
    }
}

Another way (creating a new task):

tasks.register<Test>("MyTests") {
    group = "MyCustomTasks"
    filter {
        includeTestsMatching("ir.mahozad.*Convert*")
    }
}
Malignant answered 20/7, 2023 at 12:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.