Skipping Tests with Maven – 用Maven跳过测试

最后修改: 2019年 5月 5日

中文/混合/英文(键盘快捷键:t)

1. Introduction

1.绪论

Skipping tests is often a bad idea. However, there are some situations where it could be useful — maybe when we’re developing new code and want to run intermediate builds in which the tests are not passing or compiling.

跳过测试通常是一个坏主意。然而,在某些情况下,它可能是有用的–也许当我们正在开发新的代码,并想运行中间构建,其中测试没有通过或没有编译。

In these kinds of situations only, we might skip the tests to avoid the overhead of compiling and running them. Of course, consider that not running tests can lead to bad coding practices.

只有在这种情况下,我们可能会跳过测试,以避免编译和运行测试的开销。当然,考虑到不运行测试会导致不良的编码实践。

In this quick tutorial, we’ll explore all the possible commands and options to skip tests using Maven.

在这个快速教程中,我们将探讨使用Maven跳过测试的所有可能命令和选项

2. Maven Lifecycle

2.Maven 生命周期

Before getting into the details of how to skip tests, we must understand when tests are compiled or run. In the article about Maven goals and phases, we go deeper into the concept of the Maven lifecycle, but for the purpose of this article, it’s important to know that Maven can:

在讨论如何跳过测试的细节之前,我们必须了解测试何时被编译或运行。在关于Maven目标和阶段的文章中,我们深入探讨了Maven生命周期的概念,但就本文而言,我们必须知道Maven可以。

  1. Ignore tests
  2. Compile tests
  3. Run tests

In our examples, we’ll use the package phase, which includes compiling and running the tests. The options explored throughout this tutorial belong to the Maven Surefire Plugin.

在我们的例子中,我们将使用package阶段,这包括编译和运行测试。本教程中探讨的选项属于Maven Surefire Plugin

3. Using Command Line Flags

3.使用命令行标志

3.1. Skipping the Test Compilation

3.1.跳过测试编译

First, let’s look at an example of a test that doesn’t compile:

首先,让我们看看一个不能编译的测试的例子。

@Test
public void thisDoesntCompile() {
    baeldung;
}

When we run the command-line command:

当我们运行命令行命令时。

mvn package

We’ll get an error:

我们会得到一个错误。

[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] /Users/baeldung/skip-tests/src/test/java/com/antmordel/skiptests/PowServiceTest.java:[11,9] not a statement
[INFO] 1 error

Therefore, let’s explore how to skip the compilation phase for the test’s sources. In Maven, we can use the maven.test.skip flag:

因此,让我们探讨一下如何跳过测试源的编译阶段。在Maven中,我们可以使用maven.test.skipflag。

mvn -Dmaven.test.skip package

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

结果是,测试源没有被编译,因此也没有被执行。

3.2. Skipping the Test Execution

3.2.跳过测试执行

As a second option, let’s see how we can compile the test folder but skip the run process. This is useful for those cases where we’re not changing the signature of the methods or classes but we’ve changed the business logic, and as a result, we broke the tests. Let’s consider a contrived test case like the one below, which will always fail:

作为第二种选择,让我们看看我们如何编译测试文件夹但跳过运行过程。这对于那些我们没有改变方法或类的签名,但我们改变了业务逻辑,结果是我们破坏了测试的情况是很有用的。让我们考虑一下像下面这样一个精心设计的测试案例,它总是会失败。

@Test
public void thisTestFails() {
    fail("This is a failed test case");
}

Since we included the statement fail(), if we run the package phase, the build will fail with the error:

由于我们包含了fail()语句,如果我们运行package阶段,构建将会失败,并出现错误。

[ERROR] Failures:
[ERROR]   PowServiceTest.thisTestFails:16 This is a failed test case
[INFO]
[ERROR] Tests run: 2, Failures: 1, Errors: 0, Skipped: 0

Let’s imagine we want to skip running the tests but still we want to compile them. In this case, we can use the -DskipTests flag:

让我们设想一下,我们想跳过运行测试,但我们仍然想编译它们。在这种情况下,我们可以使用-DskipTests标志。

mvn -DskipTests package

and the package phase will succeed. Also, in Maven, there is a dedicated plugin to run integration tests called maven failsafe plugin. The -DskipTests will skip the execution of both unit tests (surefire) and integration tests (failsafe). In order to just skip the integration tests, we can pass the -DskipITs system property.

而打包阶段会成功。另外,在Maven中,有一个专门用于运行集成测试的插件,叫做maven故障安全插件-DskipTests将跳过单元测试(surefire)和集成测试(failafafe)的执行。为了只跳过集成测试,我们可以通过-DskipITss系统属性。

Finally, it’s worth mentioning that the now-deprecated flag -Dmaven.test.skip.exec will also compile the test classes but will not run them.

最后,值得一提的是,现在已经过时的标志-Dmaven.test.skip.exec也会编译测试类,但不会运行它们。

4. Using Maven Configuration

4.使用Maven配置

In the case that we need to exclude compiling or running the tests for a longer period of time, we can modify the pom.xml file in order to include the proper configuration.

在我们需要排除编译或运行测试较长时间的情况下,我们可以修改pom.xml文件,以包括适当的配置。

4.1. Skipping the Test Compilation

4.1.跳过测试编译

As we did in the previous section, let’s examine how we can avoid compiling the test folder. In this case, we’ll use the pom.xml file. Let’s add the following property:

正如我们在上一节所做的那样,让我们研究一下我们如何避免编译测试文件夹。在这种情况下,我们将使用pom.xml文件。让我们添加以下属性。

<properties>
    <maven.test.skip>true</maven.test.skip>
</properties>

Keep in mind that we can override that value by adding the opposite flag in the command line:

请记住,我们可以通过在命令行中添加相反的标志来覆盖这个值

mvn -Dmaven.test.skip=false package

4.2. Skipping the Test Execution

4.2.跳过测试执行

Again, as a second step, let’s explore how we can build the test folder but skip the test execution using the Maven configuration. In order to do that, we have to configure the Maven Surefire Plugin with a property:

同样,作为第二步,让我们探讨一下我们如何利用Maven配置构建测试文件夹但跳过测试执行。为了做到这一点,我们必须为Maven Surefire插件配置一个属性。

<properties>
    <tests.skip>true</tests.skip>
</properties>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.22.2</version>
    <configuration>
        <skipTests>${tests.skip}</skipTests>
    </configuration>
</plugin>

The Maven property tests.skip is a custom property that we previously defined. Therefore, we can override it if we want to execute the tests:

Maven属性tests.skip是我们之前定义的一个自定义属性。因此,如果我们想执行测试,可以重写它。

mvn -Dtests.skip=false package

4. Conclusion

4.总结

In this quick tutorial, we’ve explored all the alternatives that Maven offers in order to skip compiling and/or running the tests.

在这个快速教程中,我们探讨了Maven提供的所有替代方案,以便跳过编译和/或运行测试。

We went through the Maven command line options and the Maven configuration options.

我们研究了Maven命令行选项和Maven配置选项。