Apache Maven Surefire 是 Maven 构建工具中的一个插件,专门用于在 Maven 构建生命周期中执行单元测试

Apache Maven Surefire 是一个测试框架项目,旨在提供 Maven 项目的单元测试支持。该聚合 POM 包含多个模块,如 SureFireLoggerAPI、SureFireAPI 和 MavenSurefirePlugin,用于处理测试执行和报告。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Bol5261

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值