Gradle – How to exclude some tests

In this tutorial, we will show you a few examples to exclude some tests in Gradle. Review the following two unit test classes

1. com.mkyong.helloworld.TestController.class
2. com.mkyong.example.TestExample.class

1. Package level RegEx

1. Any test classes from this package com/mkyong/example/ will be excluded.

build.gradle

test {
    exclude 'com/mkyong/example/**'
}

In this example, test class TestExample.class will be excluded.

Note
The package is defined with backslash, not period or dot (.), if you define com.mkyong.example.**, NO test classes will be excluded.

2. Class name RegEx

Any test classes from any package with this class name pattern *Controller* will be excluded.

build.gradle

test {
    exclude '**/*Controller*'
}

In this example, Test class TestController.class will be excluded.

Note
The RegEx pattern is case sensitive, if you define a lower case ‘c’ , like **/*controller*, NO test classes will be excluded.

3. Single Test

In this example, only the TestController.class will be excluded.

build.gradle

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

or, use the exact location.

build.gradle

test {
    exclude 'com/mkyong/helloworld/TestController.class'
}

Done.

References

  1. Gradle display test results in console
  2. Gradle – How to skip unit test
  3. Gradle Test documentation

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

avatar