Ways to skip test case execution in gradle project build


With gradle build or run command, by default executes test case execution. Sometimes, We want to disable or ignore test execution in gradle project execution.

Gradle exclude and ignore test case execution

In Maven, you can easily do with command line with option -DskipTests=true.

This blog talks about different ways to disable test cases execution in gradle project. This will skip the following test cases

  • Unit test cases
  • Integration test

is -DskipTests available with gradle command?

No it is not available

Exclude gradle tasks with test

normally when you run gradle build command,

Here is an sequence of tasks executed

gradle build
 Task :compileJava 
> Task :processResources 
> Task :classes 
> Task :jar
> Task :assemble 
> Task :compileTestJava 
> Task :processTestResources 
> Task :testClasses 
> Task :test 
> Task :check 
> Task :build 

gradle has an option to exclude(-x) or --exclude-task an task during build.

test is an inbuilt in task for executing test code.

gradle build -x test 
or
gradle build --exclude-task test

Here is an output of the running tasks

> Task :compileJava NO-SOURCE 
> Task :processResources NO-SOURCE 
> Task :classes UP-TO-DATE 
> Task :jar 
> Task :assemble 
> Task :check 
> Task :build

test source code is not run ie compileTestJava testClasses test tasks are ignored and not executed. This does not work for multi module project

Suppose 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

So you have to run two commands to to disable test case execution in multi modular project

gradle build -x :module1:test
gradle build -x :module2:test

Suppose you have multi module project

Build gradle script to disable test case compile and executioin

We can disable the test case execution using some property configuration in build script.

Now we are going to skip test case execution for test task based on onlyIf condition.

let’s add skipTest boolean property

test.onlyIf { ! Boolean.getBoolean(skipTests) }

Next, run gradle command with boolean property with -D

gradle build -DskipTests=true

This will not execute test tasks while executing gradle build project.

How to execute unit test with package or class name in gradle

This is another way to exclude test case execution using

  • package name
  • class name
  • regular expression

in build.gradle, You can add exclude property test classes with either package or class or regular expression.

test{
exclude 'com/'
exclude '**/EmpTest.class'
exclude '**/**UnitTest'
}

Conclusion

In this tutorial, Learned about Skip test case execution in gradle projects

  • single and multi module project with command line
  • build script based on ngIf condition
  • Adding test class exclude with build script with package or class name or regular expression
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.