Generic Top

I just announced the new Learn Spring course, focused on the fundamentals of Spring 5 and Spring Boot 2:

>> CHECK OUT THE COURSE

1. Introduction

Although skipping tests is usually a bad idea, there are some situations where it might be useful, and it will save us some time. For instance, consider we are developing a new feature, and we want to see a result within the intermediate builds. In this case, we might skip the tests temporarily to reduce the overhead of compiling and running them. Undoubtedly, ignoring the tests can cause many serious issues.

In this short tutorial, we'll see how to skip tests when using the Gradle build tool.

2. Using Command Line Flags

First, let's create a simple test that we want to skip:

@Test
void skippableTest() {
    Assertions.assertTrue(true);
}

When we run the build command:

gradle build

We'll see running tasks:

> ...
> Task :compileTestJava
> Task :processTestResources NO-SOURCE
> Task :testClasses
> Task :test
> ...

To skip any task from the Gradle build, we can use the -x or –exclude-task option. In this case, we'll use “-x test” to skip tests from the build.

To see it in action, let's run the build command with -x option:

gradle build -x test

We'll see running tasks:

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

As a result, the test sources are not compiled and, therefore, are not executed.

3. Using the Gradle Build Script

We have more options to skip tests using the Gradle build script. For example, we can skip tests based on some condition or only in a particular environment using the onlyIf() method. Tests will be skipped if this method returns false.

Let's skip tests based on checking a project property:

test.onlyIf { !project.hasProperty('someProperty') }

Now, we'll run the build command and pass someProperty to Gradle:

gradle build -PsomeProperty

Therefore, Gradle skips running the tests:

> ...
> Task :compileTestJava 
> Task :processTestResources NO-SOURCE 
> Task :testClasses 
> Task :test SKIPPED 
> Task :check UP-TO-DATE 
> ...

Moreover, we can exclude tests based on their package or class name using the exclude property in our build.gradle file:

test {
    exclude 'org/boo/**'
    exclude '**/Bar.class'
}

We can also skip tests based on a regex pattern. For instance, we can skip all tests whose class name ends with the word “Integration“:

test {
    exclude '**/**Integration'
}

4. Conclusion

In this tutorial, we've learned how to skip tests when using the Gradle build tool. We also went through all the relevant options that we can use on the command-line, as well as those we can use in Gradle build scripts.

Generic bottom

I just announced the new Learn Spring course, focused on the fundamentals of Spring 5 and Spring Boot 2:

>> CHECK OUT THE COURSE
2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Ron Jacobs
Ron Jacobs
25 days ago

In the spirit of completeness, another way to avoid running all tests is to bring the command line option into the build.gradle file:

gradle.startParameter.excludedTaskNames += “test”

Loredana Crusoveanu
11 days ago
Reply to  Ron Jacobs

Thanks for adding this option, Ron.

Comments are closed on this article!