1. Introduction
1.绪论
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.
Gradle近年来已成为一种非常流行的依赖管理工具,尤其是在Java开发人员中。它很容易学习,而且由于它是用Groovy编写的,所以它的可扩展性也很强。
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.
在这个快速教程中,我们将看看Gradle test 和 check 任务之间的区别。我们将确定每一个任务的确切作用,以及何时使用它们才有意义。
2. Overview of Gradle
2.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.
Gradle是一个依赖性管理工具。它为定义软件项目中的依赖关系提供了强大的功能,同时还提供了各种功能来管理从测试到部署的整个构建生命周期。
To accomplish its work, Gradle uses tasks. A task is essentially a set of discrete steps to accomplish a goal. Some examples are:
为了完成其工作,Gradle使用了任务。一个任务本质上是一组离散的步骤来完成一个目标。一些例子是
- 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.
虽然Gradle是可扩展的,允许我们定义任何我们想要的任务,但一个Java开发者想要的最常见的任务是默认可用的。其中两个任务名为test和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
3.何时使用Gradle测试
To run the test task, we would simply run:
要运行test任务,我们将简单地运行。
gradle test
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.
test任务执行项目中所有的单元测试。test任务有相当多的属性来控制它的行为,我们不会在这里全部介绍。
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.
默认情况下,test任务将自动检测项目中的所有单元测试,编译它们,然后执行它们。最后,它会生成一份报告,说明哪些测试通过了,哪些没有通过。
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.
test任务是一个伟大的通用任务,所有开发人员在使用Gradle时都应该熟悉它。通常情况下,每个开发者在向主分支提交任何新代码之前,都应该执行这个任务。
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
4.何时使用Gradle检查
To run the check task, we would simply run:
要运行检查任务,我们将简单地运行。
gradle check
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.
与test不同,check任务被称为 “生命周期 “任务。这意味着,就其本身而言,它不做任何事情。相反,它执行一个或多个其他任务。
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.
默认情况下,check任务只执行test任务。这意味着,在没有任何其他插件的情况下,这两个任务的行为方式完全相同,并产生相同的输出。
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.
check之所以重要,是因为它可以将一个或多个验证任务聚合在一起。这使我们能够将多个任务纳入一个步骤,而不是一个一个地执行它们。
The Gradle ecosystem contains a number of plugins, such as the checkstyle plugin, that provide additional functionality to the check task.
Gradle 生态系统包含许多插件,例如checkstyle插件,它们为check任务提供了额外的功能。
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.
一般来说,任何从事源代码验证工作的任务都应该附加到check任务中。像源代码风格的执行、库的漏洞扫描和集成测试都是很好的候选任务,可以与check任务一起包含。
5. Conclusion
5.总结
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.
在这篇文章中,我们仔细研究了Gradle的test和check任务。虽然它们很相似,有时可以互换使用,但它们确实有不同的目的。
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.
test任务是大多数开发者应该熟悉的任务,因为它执行我们所有的单元测试,并提供一份关于哪些测试通过和失败的报告。
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.
相比之下,check任务将test任务与其他任务相结合。有一些插件在check任务中加入了自己的步骤,但我们总是可以自己创建,以满足我们软件项目的具体需要。