It is a short tutorial on how to run single test case execution in gradle build
- Single module project
- Multi modular project
How to run single test cases in the Gradle project?
Suppose you have test EmployeeTest
java class located in com.cloudhadoop.emp
package.
It contains two test methods testEmployeeName
and testEmployeeSalary
and did not write test implementation and show for your information.
package com.cloudhadoop.emp;
class EmployeeTest{
private void testEmployeeName(){
//some code
}
private void testEmployeeSalary(){
//some code
}
}
So you want to execute a single test case -testEmployeeName in the above class.
You can use the below command with --test
option.
Here is an syntax
./gradlew taskname --test classname
Here is an example for test taskname
gradle test --test "com.cloudhadoop.emp.EmployeeTest.testEmployeeName"
This executes only a single test case.
we can apply different strings for –test options
-
all test cases from a given class i.e
com.cloudhadoop.emp.EmployeeTest
-
all test cases from all classes of a page -
com.cloudhadoop.emp
-
regular expression -
com.cloudhadoop.emp.*Test*
How to run one test case in a multi-modular gradle project?
You have a multi-module project like this
parent
|-----module1
|-----module2
|-----build.gradle
In parent project, if you run Gradle with -x, It does not do anything.
How do you run single test cases in the multi-module project?
gradle :module1:test --test "com.cloudhadoop.emp.EmployeeTest.testEmployeeName"
--test
option in the command line will not work if your build has different flavors and throws with the below exception in the terminal.
Unknown command-line option ‘–tests’
The solution is to change your task name from test
to ‘yourtaskname’.
How to run a single test case in Android app
In Android applications, Sometimes we want to run one unit test case execution with the command line.
gradle app:testDebug --tests=com.cloudhadoop.emp.EmployeeTest
Conclusion
You learned how to run single unit test cases execution in gradle projects.
with the command line, You need to specify the taskname and –test option with the test case package path