Quick Guide to the Maven Surefire Plugin – Maven Surefire插件的快速指南

最后修改: 2018年 4月 19日

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

1. Overview

1.概述

This tutorial demonstrates the surefire plugin, one of the core plugins of the Maven build tool. For an overview of the other core plugins, refer to this article.

本教程演示的surefire插件是Maven构建工具的核心插件之一。有关其他核心插件的概述,请参阅本文

2. Plugin Goal

2.插件目标

We can run the tests of a project using the surefire plugin. By default, this plugin generates XML reports in the directory target/surefire-reports.

我们可以使用surefire插件来运行项目的测试。默认情况下,这个插件会在target/surefire-reports目录下生成XML报告。

This plugin has only one goal, test. This goal is bound to the test phase of the default build lifecycle, and the command mvn test will execute it.

这个插件只有一个目标,test。这个目标被绑定到默认构建生命周期的test阶段,命令mvn test将执行它。

3. Configuration

3.配置

The surefire plugin can work with the test frameworks JUnit and TestNG. No matter which framework we use, the behavior of surefire is the same.

surefire插件可以与测试框架JUnit和TestNG一起工作。无论我们使用哪个框架,surefire的行为都是一样的。

By default, surefire automatically includes all test classes whose name starts with Test, or ends with Test, Tests or TestCase.

默认情况下,surefire自动包括所有名称以Test开头,或以TestTestsTestCase结尾的测试类。

We can change this configuration using the excludes and includes parameters, however:

然而,我们可以使用excludesincludes参数改变这种配置。

<plugin>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.22.2</version>
    <configuration>
        <excludes>
            <exclude>DataTest.java</exclude>
        </excludes>
        <includes>
            <include>DataCheck.java</include>
        </includes>
    </configuration>
</plugin>

With this configuration, test cases in the DataCheck class are executed while the ones in DataTest aren’t.

通过这种配置,DataCheck类中的测试用例被执行,而DataTest中的测试用例没有被执行。

We can find the latest version of the plugin here.

我们可以找到该插件的最新版本这里

4. Conclusion

4.结论

In this quick article, we went through the surefire plugin, describing its only goal as well as how to configure it.

在这篇快速文章中,我们通过surefire插件,描述了它的唯一目标以及如何配置它。

As always, the complete source code for this tutorial can be found over on GitHub.

一如既往,本教程的完整源代码可以在GitHub上找到

Next »

The Maven Deploy Plugin

« Previous

The Maven Failsafe Plugin