Gradle has become a very popular dependency management tool in recent years, especially among Java developers. It’s easy to learn, and because it’s written in Groovy, it is also very extensible.
In this quick tutorial, we’ll look at the difference between the Gradle test and check tasks. We’ll identify exactly what each one does and when it makes sense to use them.
2. Overview of Gradle
Gradle is a dependency management tool. It provides robust features for defining dependencies in a software project, along with a variety of functions to manage the entire build lifecycle, from testing to deployment.
To accomplish its work, Gradle uses tasks. A task is essentially a set of discrete steps to accomplish a goal. Some examples are:
- Compile source code
- Package source code into a module
- Deploy modules to a remote system
While Gradle is extensible and allows us to define any task we want, the most common tasks a Java developer would want are available by default. Two of those tasks are named test and check.
While they are similar in nature, they each have slightly different roles when it comes to managing a software project. In the following sections, we’ll look at each one a little closer.
3. When to Use Gradle Test
To run the test task, we would simply run:
The test task executes all of the unit tests in the project. The test task has quite a few properties that control its behavior, and we won’t cover them all here.
By default, the test task will auto-detect all unit tests in the project, compile them, and then execute them. In the end, it generates a report of which tests have passed and failed.
The test task is a great general-purpose task that all developers should be familiar with when working with Gradle. Typically, it should be executed by each developer before committing any new code to the main branch.
But as an extra layer of protection, it’s also common to automatically execute it as part of any formal software build process. For example, it’s common to utilize the test task as part of any automated software build after compiling the source code but prior to assembling the final artifacts.
4. When to Use Gradle Check
To run the check task, we would simply run:
Unlike test, the check task is referred to as a “lifecycle” task. This means that, on its own, it does not do anything. Instead, it executes one or more other tasks.
By default, the check task only executes the test task. This means that, in the absence of any other plugins, the two tasks behave exactly the same way and generate the same output.
The reason check is important is because it can aggregate one or more verification tasks together. This allows us to incorporate multiple tasks into a single step rather than having to execute them one by one.
The Gradle ecosystem contains a number of plugins, such as the checkstyle plugin, that provide additional functionality to the check task.
In general, any task that does source code verification work should be attached to the check task. Things like source code style enforcement, library vulnerability scans, and integration tests are all good candidates to include with the check task.
In this article, we’ve taken a closer look at the Gradle test and check tasks. While they’re similar and can sometimes be used interchangeably, they do serve different purposes.
The test task is one most developers should be familiar with, as it executes all of our unit tests and provides a report on which ones are passing and failing.
In contrast, the check task combines the test task with other tasks. There are a number of plugins that add their own steps to the check task, but we can always create our own to meet the specific needs of our software project.