Apache Maven Surefire 是 Maven 构建工具中的一个插件,专门用于在 Maven 构建生命周期中执行单元测试。它支持多种测试框架(如 JUnit、TestNG 等),并允许您配置和运行测试,生成测试报告。以下是关于 Apache Maven Surefire 的详细介绍和常见用法:
1. Maven Surefire 的作用
- 执行单元测试:在 Maven 的
test
生命周期阶段运行测试。 - 生成测试报告:将测试结果输出到控制台,并生成详细的测试报告(通常位于
target/surefire-reports
目录中)。 - 支持多种测试框架:默认支持 JUnit 和 TestNG,也可以通过插件扩展支持其他框架。
2. Maven Surefire 的配置
在 Maven 项目的 pom.xml
文件中,可以通过 <plugin>
配置 Maven Surefire 插件。以下是一个基本配置示例:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M7</version> <!-- 使用最新版本 -->
<configuration>
<skipTests>false</skipTests> <!-- 是否跳过测试 -->
<testFailureIgnore>false</testFailureIgnore> <!-- 是否忽略测试失败 -->
<includes>
<include>**/*Test.java</include> <!-- 包含的测试类 -->
</includes>
<excludes>
<exclude>**/*IntegrationTest.java</exclude> <!-- 排除的测试类 -->
</excludes>
</configuration>
</plugin>
</plugins>
</build>
3. 常用配置选项
配置项 | 描述 |
---|---|
<skipTests> | 是否跳过测试,默认值为 false 。 |
<testFailureIgnore> | 是否忽略测试失败,默认值为 false 。 |
<includes> | 指定要运行的测试类(支持通配符)。 |
<excludes> | 指定要排除的测试类(支持通配符)。 |
<forkCount> | 并行运行测试的进程数。 |
<reuseForks> | 是否重用测试进程,默认值为 true 。 |
<argLine> | 传递给 JVM 的参数(如内存设置)。 |
<systemPropertyVariables> | 设置系统属性,可以在测试中通过 System.getProperty() 获取。 |
4. 运行测试
在 Maven 项目中,可以通过以下命令运行测试:
mvn test
- 如果只想运行特定测试类,可以使用
-Dtest
参数:mvn test -Dtest=MyTestClass
- 如果只想运行特定方法,可以使用
-Dtest
参数:mvn test -Dtest=MyTestClass#myTestMethod
5. 生成测试报告
Maven Surefire 会在 target/surefire-reports
目录中生成测试报告,包括:
- 文本格式报告:
*.txt
文件。 - XML 格式报告:
*.xml
文件,可用于集成到 CI/CD 工具中。
6. 常见问题
跳过测试
- 在构建时跳过测试:
mvn install -DskipTests
- 在
pom.xml
中永久跳过测试:<configuration> <skipTests>true</skipTests> </configuration>
并行测试
- 配置并行测试:
<configuration> <forkCount>2</forkCount> <!-- 并行进程数 --> <reuseForks>true</reuseForks> </configuration>
测试失败时继续构建
- 即使测试失败也继续构建:
<configuration> <testFailureIgnore>true</testFailureIgnore> </configuration>
7. 扩展支持
Maven Surefire 支持通过插件扩展支持其他测试框架,例如:
- JUnit 5:需要添加
junit-platform-surefire-provider
依赖。 - Spock:需要添加
spock-core
依赖。
8. 参考文档
通过以上内容,您可以更好地理解和使用 Apache Maven Surefire 插件来管理和运行单元测试。
Fork me on GitHub
Overview
Modules
Project Documentation
Maven Projects
ASF
Apache Maven Surefire
Apache/ Maven/ Surefire/ About
| Last Published: 2019-11-13
Version: 3.0.0-M4
Overview
Introduction
License
Download
Modules
SureFire Logger API
SureFire API
Surefire Extensions API
SureFire Booter
Maven Surefire Test-Grouping Support
SureFire Providers
ShadeFire JUnit3 Provider
Maven Surefire Common
Surefire Report Parser
Maven Surefire Plugin
Maven Failsafe Plugin
Maven Surefire Report Plugin
Maven Surefire Integration Tests
Surefire Shared Utils
Project Documentation
Project Information
About
Summary
Dependency Information
Project Modules
Team
Source Code Management
Issue Management
Mailing Lists
Dependency Management
Dependencies
Dependency Convergence
CI Management
Plugin Management
Plugins
Distribution Management
Project Reports
Maven Projects
Archetype
Artifact Resolver
Doxia
JXR
Maven
Parent POMs
Plugins
Plugin Testing
Plugin Tools
Resource Bundles
SCM
Shared Components
Skins
Surefire
Wagon
ASF
How Apache Works
Foundation
Sponsoring Apache
Thanks
Follow ASFMavenProject
Built by Maven
About Apache Maven Surefire
Surefire is a test framework project. This is the aggregator POM in Apache Maven Surefire project.
Project Modules
This project has declared the following modules:
Name Description
SureFire Logger API Interfaces and Utilities related only to internal SureFire Logger API. Free of dependencies.
SureFire API API used in Surefire and Failsafe MOJO, Booter, Common and test framework providers.
Surefire Extensions API Surefire is a test framework project. This is the aggregator POM in Apache Maven Surefire project.
SureFire Booter API and Facilities used by forked tests running in JVM sub-process.
Maven Surefire Test-Grouping Support Maven Surefire Test-Grouping Support
SureFire Providers SureFire Providers Aggregator POM
ShadeFire JUnit3 Provider A super-shaded junit3 provider that is used by surefire to build itself, that basically has ALL classes relocated to facilitate no API-conflict whatsoever with ourself. The only remaining point of conflict is around the booter properties file format.
Maven Surefire Common API used in Surefire and Failsafe MOJO.
Surefire Report Parser Parses report output files from surefire.
Maven Surefire Plugin Maven Surefire MOJO in maven-surefire-plugin.
Maven Failsafe Plugin Maven Failsafe MOJO in maven-failsafe-plugin.
Maven Surefire Report Plugin Maven Surefire Report MOJO in maven-surefire-report-plugin.
Maven Surefire Integration Tests Used internally testing MOJOs. The project is not deployed.
Surefire Shared Utils Relocated Java packages of maven-shared-utils in Surefire
Apache Apache Maven Surefire, Apache Maven Surefire, Apache, the Apache feather logo, and the Apache Apache Maven Surefire project logos are trademarks of The Apache Software Foundation.
Privacy Policy