How to run only single test cases with gradle build


This is an short tutorial on how to run single test case execution in gradle build

  • Single module project
  • Multi modular project

Run only single one test case in gradle build some times, to debug testcase execution, We need to run single test cases for multiple times, thus helps 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 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 showing for your information

package com.cloudhadoop.emp;
class EmployeeTest{
 private void testEmployeeName(){
     //some code

 }
 private void testEmployeeSalary(){
     //some code
     
 }
}

So you want to execute single test case -testEmployeeName in the above class.

You can use 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 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 cases in multi modular gradle project?

You have multi module project like this

parent 
|-----module1
|-----module2
|-----build.gradle

In parent project, if you run gradle with -x, It does not do nothing

How do you run single test cases in multi module project

gradle :module1:test --test "com.cloudhadoop.emp.EmployeeTest.testEmployeeName"

--test option in command line will not work if your build has different flavours and throws with below exception in terminal.

Unknown command-line option ‘–tests’

The solution is to change your taskname from test to ‘yourtaskname’

How to run single test case in Android app

In Android application, Sometimes we want to run one unit test case execution with 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 command line, You need to specify the taskname and –test option with testcase package path

THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.