HomeGradleSkip tests with Gradle?

Skip tests with Gradle?

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 or tasks from build.

Running gradle build without tests is quite often. For example, in DEV environment to make a build quickly, you may need to skip tests.

1. Skip Unit tests from Gradle build

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

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

> 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

> 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 build skip tests

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 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 or tasks 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 in eclipse
  2. Gradle System Properties
  3. Gradle – run specific tests
  4. which method is used to attach a predicate to a skipping task
  5. Maven Skip Tests

5. Resources

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

LEAVE A REPLY

Please enter your comment!
Please enter your name here