Home gradle Gradle - How to skip / exclude unit tests

Gradle – How to skip / exclude unit tests

In this tutorial you will see several examples using Gradle, 1. How to skip test / task, 2. Skip unit tests based on build script 3. Skip OR Exclude specific unit tests from build.

1. Skip Unit tests from Gradle build

To skip unit tests from gradle build you can use the -x or --exclude-task option. -i is optional, it is used to get detailed log info of each task. By using -x option you can skip any gradle task as well.

gradle build -x test -i     OR    gradle build --exclude-task test -i

Let’s Debug the Task List :

1. Task List for default build :

$ gradle build -i

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

BUILD SUCCESSFUL in 11s

2. Task List when you skip task using -x option

$ gradle build -x test -i

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

BUILD SUCCESSFUL in 6s

For clear understanding have a look into following task dependency graph, if you skip any task the sub dependency tasks will be skipped. You could try with commands gradle build -x check -i and gradle build -x testClasses -i and verify completed tasks list.

gardle skip test

2. Skip Unit tests using Gradle build script

For example you want to skip tests based on condition or skip only on particular environment you can use the onlyIf() method to attach a predicate to a task in build script. Tests will be SKIPPED if onlyIf() return false.

For example if you want to skip tests on production environment, add following code in build.gradle file.

// Skips tests if you provide prod project property from command line
test.onlyIf { !project.hasProperty('prod') }

Command to run build :

You run the following command and verify task list.

$ gradle build -Pprod -i

$ gradle build -Pprod -i

> Task :compileJava UP-TO-DATE
> Task :processResources NO-SOURCE
> Task :classes UP-TO-DATE
> Task :jar UP-TO-DATE
> Task :assemble UP-TO-DATE
> Task :compileTestJava UP-TO-DATE
> Task :processTestResources UP-TO-DATE
> Task :testClasses UP-TO-DATE
> Task :test SKIPPED
Skipping task ':test' as task onlyIf is false.
> Task :check UP-TO-DATE
> Task :build UP-TO-DATE


BUILD SUCCESSFUL in 9s

3. Skip / Exclude specific tests in Gradle

To exclude tests explicitly add the following following code in build.gradle and run build using gradle build -i. You can use regular expressions for class names or package names pattern matching.

3.1. Exclude tests from Specific package :

test {
    exclude 'com/javabydeveloper/test/payment/**'
}

3.2. Exclude Specific test class :

test {
    exclude 'com/javabydeveloper/test/payment/Junit5_Payment_Test.class'
}

                          OR

test {
    exclude '**/Junit5_Payment_Test.class'
}

4. Conclusion

In this tutorial we have covered several examples using Gradle, 1. How to skip unit tests, 2. Skip unit tests based on build script 3. Skip OR Exclude specific unit tests from build.

You also might interested in our other Gradle Tutorials :

  1. How to create Gradle project and run build in eclipse
  2. Gradle System Properties
  3. Gradle – run specific tests

5. Resources

  1. Gradle Document java test
  2. Gradle task document
  3. Gradle test Document

Satish Varma
Satish Varmahttps://javabydeveloper.com
Satish is post graduated in master of computer applications and experienced software engineer with focus on Spring, JPA, REST, TDD and web development. Also founder of javabydeveloper.com. Follow him on LinkedIn or Twitter or Facebook

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Stay in Touch

Categories

Related Articles