How to run a specific unit test class using Gradle?

How can I run a specific unit test class using Gradle? I’m new to Gradle (version 1.10) and Ubuntu 13. Is there a command similar to testOnly in SBT that allows executing just one unit test class?

Hey Ariyas,

To run a specific unit test class in Gradle, you can use the following command-line options. Gradle 1.10 supports selecting tests using a test filter: To run a specific test method: gradle test --tests org.gradle.SomeTest.someSpecificFeature To run a specific test class with a wildcard:

gradle test --tests ‘*SomeTest.someSpecificFeature’ gradle test --tests ‘SomeSpecificTest’ gradle test --tests 'all.in.specific.package

To run tests based on naming conventions:

gradle test --tests ‘IntegTest’ gradle test --tests 'IntegTestui’ gradle test --tests ‘*IntegTest.singleMethod’

For multiple tasks: gradle someTestTask --tests ‘*UiTest’ someOtherTestTask --tests ‘WebTestui’

In your build.gradle file, you can also configure test filters:

apply plugin: ‘java’

test { filter { // Specific test method includeTestsMatching “org.gradle.SomeTest.someSpecificFeature”

// Specific test class
includeTestsMatching "*SomeTest"

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

// All integration tests
includeTestsMatching "*IntegTest"

// Only UI tests from integration tests
includeTestsMatching "*IntegTest*ui"

} }

Note that for multi-flavor environments, such as Android, the --tests argument might not be supported and could result in an error.

Hey Ariyas,

After some troubleshooting, the following command worked for me:

gradle test --tests “a.b.c.MyTestFile.mySingleTest”

Hi there! :blush:

I’m glad you’re seeking solutions for your testing needs. Here’s what worked for me:

For release builds:

gradle testReleaseUnitTest --tests testClass

For debug builds:

gradle testDebugUnitTest --tests AsyncExecutorTest

If you need to list all the projects, you can run:

gradle -q projects

After that, just navigate to the specific project that contains the test class you want to execute.

Hope this helps, and happy coding! :rocket: