Main Tutorials

Gradle – How to exclude some tests

There are two ways to exclude some unit tests in Gradle.

Topics.

P.S Tested with Gradle 6.7.1

1. Gradle Test filtering

Gradle test filtering excludeTest and excludeTestsMatching (support regex) examples to show you how to exclude some tests.

build.gradle

apply plugin: 'java'

 test {
     filter {

        // exclude specific method in any of the tests
        // method name like `test_stress_passed()`, test_stress_timeout()` in any tests will be excluded.
        // this is flexible and powerful filtering, need extra care.
        excludeTestsMatching "*stress*"

        //exclude all tests from a package.
        excludeTestsMatching "com.mkyong.security.*"

        //exclude a single test class.
        excludeTestsMatching "com.mkyong.SomeTest"

        // exclude a single method of a test class, excludeTestsMatching version.
        excludeTestsMatching "com.mkyong.SomeTest.someTestMethod"

        // exclude a single method of test class, excludeTest version.
        excludeTest "com.mkyong.SomeTest", "someTestMethod"

        // exclude all tests containing the `integration` file path or package.
        excludeTestsMatching "*.integration.*"

        // of course, Gradle also has include, opposite to exclude.
        // includeTestsMatching "com.mkyong.security.*"
        // includeTest "com.mkyong.SomeTest", "someTestMethod"

     }
 }

1.1 Test classes

Review a list of the test classes, and later we will show how to use the test filtering to exclude some tests.

Terminal

com/mkyong/security/db/IpTableTest.java
com/mkyong/security/rule/BadAgentRuleTest.java
com/mkyong/security/rule/GoodAgentRuleTest.java
com/mkyong/integration/action/BadIpTest.java

1.2 Exclude tests from a package

The below example excludes all tests from this package com.mkyong.integration.*.

build.gradle

apply plugin: 'java'

 test {
     filter {

        //exclude all tests from a package.
        excludeTestsMatching "com.mkyong.integration.*"

     }
 }

Result.

Terminal

com/mkyong/security/db/IpTableTest.java         (run this)
com/mkyong/security/rule/BadAgentRuleTest.java  (run this)
com/mkyong/security/rule/GoodAgentRuleTest.java (run this)
com/mkyong/integration/action/BadIpTest.java    (excluded)

1.3 Exclude a single test using a class name

The below example excludes a single test class using the exact class name BadAgentRuleTest.

build.gradle

apply plugin: 'java'

 test {
     filter {

        //exclude a single test class.
        excludeTestsMatching "*.BadAgentRuleTest"

        // or like this
        // excludeTest "com.mkyong.security.rule.BadAgentRuleTest"

     }
 }

Result.

Terminal

com/mkyong/security/db/IpTableTest.java         (run this)
com/mkyong/security/rule/BadAgentRuleTest.java  (excluded)
com/mkyong/security/rule/GoodAgentRuleTest.java (run this)
com/mkyong/integration/action/BadIpTest.java    (run this)

1.4 Exclude a single method of a test class

The below example excludes a single method of a test class.

A dummy test class containing two methods test_a_ok and test_b_ok.

DummyTest.java

package com.mkyong.security.db;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertTrue;

public class DummyTest {

  @Test
  void test_a_ok() {
      assertTrue(true);
  }

  @Test
  void test_b_ok() {
      assertTrue(true);
  }

}

Test filtering to exclude a single method test_a_ok from the DummyTest.

build.gradle

apply plugin: 'java'

 test {
     filter {

       // exclude the `test_a_ok` method.
       excludeTestsMatching "*.db.DummyTest.test_a_ok"

       // excludeTest "com.mkyong.security.db.DummyTest", "test_a_ok"

     }
 }

1.5 Exclude test methods contains a specific method name

The below example excludes test methods that containing the method name bad_uri.

Review another test class.

Bad404SecurityRuleTest.java

package com.mkyong.security.rule;

//...

public class Bad404SecurityRuleTest {

  @Test
  void test_bad_uri_404_invalid(String requestUri) {
      //...
  }

  @Test
  void test_bad_uri_200_valid(String requestUri) {
      //...
  }

  @Test
  void test_good_uri_404_valid(String requestUri) {
      //...
  }

}

Add exclude testing filtering:

build.gradle

apply plugin: 'java'

 test {
     filter {

       // run this test
       includeTestsMatching "*.Bad404SecurityRuleTest"

       // exclude any test methods containing the method name `bad_uri`
       excludeTestsMatching "*bad_uri*"

     }
 }

In the above example, the Gradle will skip the test methods test_bad_uri_404_invalid and test_bad_uri_200_valid, and only run the test method test_good_uri_404_valid.

2. Gradle include and exclude tests

In the old day, we use include and exclude to exclude tests, and this way is fewer features and flexibility compared to the above test filtering.

build.gradle

apply plugin: 'java'

test {

    // exclude tests from a package.
    exclude 'com/mkyong/security/rule/**'

    // exclude a single test using the class name.
    exclude `com/mkyong/security/db/IpTableTest.class`

    // exclude tests that matches this folder patterns.
    exclude `**/integration/**`

    // include examples
    // include 'com/mkyong/security/rule/**'
    // include `com/mkyong/security/db/IpTableTest.class`

}

2.1 Test classes

Review a list of test classes, and later we will show how to use exclude to exclude some of the tests.

Terminal

com/mkyong/security/db/IpTableTest.java
com/mkyong/security/rule/BadAgentRuleTest.java
com/mkyong/security/rule/GoodAgentRuleTest.java
com/mkyong/integration/action/BadIpTest.java

2.2 Exclude tests from a package

The below example excludes all tests from the package com/mkyong/security/rule/

build.gradle

apply plugin: 'java'

test {
    exclude 'com/mkyong/security/rule/**'
}

Result

Terminal

com/mkyong/security/db/IpTableTest.java         (run this)
com/mkyong/security/rule/BadAgentRuleTest.java  (excluded)
com/mkyong/security/rule/GoodAgentRuleTest.java (excluded)
com/mkyong/integration/action/BadIpTest.java    (run this)

2.3 Exclude a single test using the class name

The below example excludes a single test with the exact class name.

build.gradle

apply plugin: 'java'

test {
    exclude `com/mkyong/security/db/IpTableTest.class`
}

Result

Terminal

com/mkyong/security/db/IpTableTest.java         (excluded)
com/mkyong/security/rule/BadAgentRuleTest.java  (run this)
com/mkyong/security/rule/GoodAgentRuleTest.java (run this)
com/mkyong/integration/action/BadIpTest.java    (run this)

2.4 Exclude tests that match a specified pattern

The below example excludes tests that match this pattern **/integration/**.

build.gradle

apply plugin: 'java'

test {
    exclude `**/integration/**`
}

Result

Terminal

com/mkyong/security/db/IpTableTest.java         (run this)
com/mkyong/security/rule/BadAgentRuleTest.java  (run this)
com/mkyong/security/rule/GoodAgentRuleTest.java (run this)
com/mkyong/integration/action/BadIpTest.java    (excluded)

2.5 include examples

The below example shows the use of include. The include does the opposite of exclude; it tells Gradle only to run the included tests.

build.gradle

apply plugin: 'java'

test {
    include `**/rule/**`
}

Result

Terminal

com/mkyong/security/db/IpTableTest.java         (excluded)
com/mkyong/security/rule/BadAgentRuleTest.java  (included, run this)
com/mkyong/security/rule/GoodAgentRuleTest.java (included, run this)
com/mkyong/integration/action/BadIpTest.java    (excluded)

2.6 Difference between * and ** in a file path

The ** double star means any folders within the current folder, at any depth.

For examples:

Terminal

com/mkyong/**/*Rule*

The current folder is com/mkyong/, the ** means any folders at any depth, and the file name contains the pattern *Rule*.

Terminal

  com/mkyong/a/anyRuleTest      (matched)
  com/mkyong/a/b/anyRuleTest    (matched)
  com/mkyong/a/b/c/anyRuleTest  (matched)
  com/mkyong/y/z/anyRuleTest    (matched)

Refer to the previous unit test examples, and the above pattern matches both BadAgentRuleTest and GoodAgentRuleTest.

Terminal

com/mkyong/security/db/IpTableTest.java
com/mkyong/security/rule/BadAgentRuleTest.java  (matched)
com/mkyong/security/rule/GoodAgentRuleTest.java (matched)
com/mkyong/integration/action/BadIpTest.java

2.7 Exclude a singe method from a test.

Don’t support this, try test filtering.

References

About Author

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

Comments

Subscribe
Notify of
4 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Tejas
3 years ago

What if I have to exclude one of test in the TestExample.class? Please help me

Budzik
1 year ago
Reply to  Tejas

Hello
Thank you mkyong for the great article 🙂
Example for build.gradle.kts

tasks.test{

filter{

//skip classes test with name ends IntegrationTest

excludeTestsMatching(“**.*IntegrationTest”)

//skip classes test with name contains Real

excludeTestsMatching(“**.*Real*”)

//skip methods test name contains (INTEGRATION)

excludeTestsMatching (“*(INTEGRATION)*”)

}

}

Last edited 1 year ago by Budzik
Tornike
1 year ago

The very important(at least for me) information here would be that, immediately when I try to apply this to Android projects, I hit a wall that I cannot use ‘java’ plugin there.

So to achieve this we need to change the approach a bit:

testOptions {
    unitTests {
        all {
            test {
                filter {
                    excludeTestsMatching 'Whatever'
                }
            }
        }
    }
}

Took me couple of days to achieve this as its not properly mentioned anywhere, so if you include this in a description maybe people will benefit.