Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run only one unit test class using Gradle

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.

like image 905
bula Avatar asked Mar 19 '14 12:03

bula


People also ask

How do I run a single test case in Java?

If we want to execute a single test class, we can execute the command mvn test -Dtest=”TestClassName”. As the output shows, only the test class we've passed to the test parameter is executed.


2 Answers

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.

like image 188
Maleen Abewardana Avatar answered Sep 22 '22 08:09

Maleen Abewardana


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.

like image 39
airborn Avatar answered Sep 21 '22 08:09

airborn