1. Introduction
1.绪论
Although skipping tests is usually a bad idea, there are some situations where it might be useful, and it saves us some time. For instance, let’s consider that we’re 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 learn how to skip tests when using the Gradle build tool.
在这个简短的教程中,我们将学习如何在使用Gradle构建工具时跳过测试。
2. Using Command Line Flags
2.使用命令行标志
First, let’s create a simple test that we want to skip:
首先,让我们创建一个我们想跳过的简单测试。
@Test
void skippableTest() {
Assertions.assertTrue(true);
}
When we run the build command:
当我们运行build命令时。
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.
要从Gradle构建中跳过任何任务,我们可以使用-x或-exclud-task选项。在本例中,我们将使用”-x test“来跳过构建中的测试。
To see it in action, let’s run the build command with -x option:
为了看到它的作用,让我们运行带有-x选项的build命令。
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 aren’t compiled, and therefore, aren’t executed.
结果是,测试源没有被编译,因此也就没有被执行。
3. Using the Gradle Build Script
3.使用Gradle构建脚本
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.
我们有更多的选择来跳过使用Gradle构建脚本的测试。例如,我们可以根据某些条件跳过测试,或者只在特定的环境中使用onlyIf()方法。如果这个方法返回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:
现在我们将运行build命令,并将someProperty传给Gradle。
gradle build -PsomeProperty
Thus, Gradle skips running the tests:
因此,Gradle跳过了运行这些测试。
> ...
> 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:
此外,我们可以使用build.gradle文件中的exclude属性,根据其包或类的名称排除测试。
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“:
我们也可以根据一个重合模式跳过测试。例如,我们可以跳过所有类名以”Integration“结尾的测试。
test {
exclude '**/**Integration'
}
4. Conclusion
4.总结
In this article, we learned how to skip tests when using the Gradle build tool. We also went through all the relevant options we can use on the command-line, as well as those we can use in Gradle build scripts.
在这篇文章中,我们学习了在使用Gradle构建工具时如何跳过测试。我们还经历了所有我们可以在命令行上使用的相关选项,以及那些我们可以在Gradle构建脚本中使用的选项。