How to run only single test cases with Gradle build
It is a short tutorial on how to run single test case execution in gradle build
- Single module project
- Multi modular project
times, to debug test case execution, We need to run single test cases multiple times, thus helping developers to debug and run multiple times without wasting time. Test cases can be unit or integration or functional tests.
How to run single test cases in the Gradle project?
Suppose you have tested the EmployeeTest
java class located in the 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 a syntax
./gradlew taskname --test classname
Here is an example of 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*
You can replace taskname=test with any existing task name.
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 the parent project, if you run Gradle with -x, It does not do anything.
How do you run single test cases in a multi-module project?
gradle :module1:test --test "com.cloudhadoop.emp.EmployeeTest.testEmployeeName"
The --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 an 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