bula
bula

Reputation: 9429

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.

Upvotes: 706

Views: 603450

Answers (15)

Forrest
Forrest

Reputation: 3490

Just in case this helps some other poor soul, my resolution was fixing a directory name that IntelliJ had "automagically" created for me.

When I generated tests, it created the following directory path: src/test/java/com/challenge/company/controller/DataControllerSpec.groovy

After a lot of frustration, I saw that "java" was a dir in the test path automatically created by my IDE. I changed it to "groovy" src/test/groovy/com/challenge/company/controller/DataControllerSpec.groovy

After that dir rename, everything worked as expected.

Project details: Groovy/Spock testing. Java/Spring Boot for project code.

Upvotes: 0

Tvisha Raji
Tvisha Raji

Reputation: 1

Yes, you can run a specific test class in Gradle by using the --tests option.

Here's how you can do it:

gradle test --tests com.example.MyTestClass

Explanation: Replace com.example.MyTestClass with the fully qualified name of your test class (including the package). Gradle will execute only the tests in the specified class.

Additional Notes:

If you want to run a specific test method within the class, you can include the method name:

gradle test --tests com.example.MyTestClass.myTestMethod

Gradle 1.10 is quite old. If possible, consider upgrading Gradle to a more recent version for better features, performance, and security.

Also, to save the hassle try using keploy's VS Code extension for unit test generation

Upvotes: 0

Kaustubha M
Kaustubha M

Reputation: 31

To run individual Gradle test in command line,

$ gradle test --tests "com.package.className.testMethodName"

Upvotes: 0

Mahozad
Mahozad

Reputation: 24692

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*")
    }
}

Upvotes: 1

Maleen Abewardana
Maleen Abewardana

Reputation: 14572

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.

Upvotes: 988

Felix
Felix

Reputation: 1296

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 

Upvotes: 21

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

Upvotes: 0

airborn
airborn

Reputation: 2625

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.

Upvotes: 222

anagaf
anagaf

Reputation: 2250

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)

Upvotes: 70

k4dima
k4dima

Reputation: 6251

Run a single test called MyTest:

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

Upvotes: 10

mutkan
mutkan

Reputation: 954

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

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

Upvotes: 4

Greg
Greg

Reputation: 2627

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

Upvotes: 0

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 

Upvotes: 8

Pratik Khadloya
Pratik Khadloya

Reputation: 12879

After much figuring out, the following worked for me:

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

Upvotes: 57

greperror
greperror

Reputation: 5676

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.

Upvotes: 180

Related Questions