Run only a Single Unit Test using Gradle

You may have a lot of Unit Test cases in for your Java Project and might want to run only a single one at a time when either you are testing it or if its the one failing.

In such a case you can run only one test case in one of the following ways.

Option 1: Using the Fully-qualified name for a Class

gradle test --tests "org.code2care.EmployeeTest"

Option 2: Using the Fully-qualified name for a Class and Method

gradle test --tests "org.code2care.EmployeeTest.validUserTest"

Option 3: Using Wildcards *

gradle test --tests "*EmployeeTest.user*"

Read More:

Note: If you are using gradle wrapper, make use of gradlew. If you are on Linux/macOS or any other Unix-like operating system, then make use of ./gradlew instead of gradlew.

Option 4: From Multiple Modules

If you have multiple modules, you can run a single test case from a specific module as below,

./gradlew :module_name:test_name
Gradle run single test case example

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail:

Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap