Maven Surefire 插件简介

发布于:2024-11-28 ⋅ 阅读:(234) ⋅ 点赞:(0)

Maven Surefire 插件是 Maven 构建系统中的一个关键组件,专门用于在构建生命周期中执行单元测试。

它通常与 Maven 构建生命周期的测试阶段绑定,确保所有单元测试在项目编译后和打包前被执行。

最新版本

Maven Surefire 插件的最新版本为 3.5.2

使用最新版本可以确保访问到最新的特性和改进。

配置 Maven Surefire 插件

要使用 Maven Surefire 插件,您需要在项目的 pom.xml 文件中进行配置。下面将通过步骤介绍如何在一个 Maven 项目中设置 Surefire 插件。

步骤 1: 创建 Maven 项目

运行以下命令创建一个新的 Maven 项目:

mvn archetype:generate -DgroupId=com.example -DartifactId=surefire-plugin-demo -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

该命令会生成一个简单的 Maven 项目,其目录结构如下:

surefire-plugin-demo
|-- src
|   |-- main
|   |   `-- java
|   |       `-- com
|   |           `-- example
|   |               `-- App.java
|   `-- test
|       `-- java
|           `-- com
|               `-- example
|                   `-- AppTest.java
|-- pom.xml
`-- target
步骤 2: 添加 Maven Surefire 插件配置

进入项目目录并打开 pom.xml 文件,在 <build> 标签内添加 Maven Surefire 插件的配置:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    
    <groupId>com.example</groupId>
    <artifactId>surefire-plugin-demo</artifactId>
    <version>1.0-SNAPSHOT</version>
    
    <dependencies>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.8.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.8.2</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0-M8</version>
            </plugin>
        </plugins>
    </build>
</project>
  • <groupId>: 指定 Maven Surefire 插件的组ID。
  • <artifactId>: 指定 Maven Surefire 插件。
  • <version>: 使用的插件版本。请确保使用最新版本。
步骤 3: 创建单元测试

src/test/java 目录下创建一个单元测试文件,例如 AppTest.java

package com.example;

import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class AppTest {

    @Test
    public void sampleTest() {
        System.out.println("正在运行单元测试...");
        assertTrue(true);
    }
}
步骤 4: 构建并运行单元测试

使用以下命令构建项目并运行单元测试:

mvn clean test

构建完成后,单元测试将在测试阶段运行。

步骤 5: 查看结果

输出将包括单元测试的结果:

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
运行 com.example.AppTest
正在运行单元测试...
测试运行数: 1, 失败数: 0, 错误数: 0, 跳过数: 0, 总耗时: 0.001 秒
结果:

测试运行数: 1, 失败数: 0, 错误数: 0, 跳过数: 0
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------

高级配置

Maven Surefire 插件提供了多种配置选项来定制测试行为。以下是一些常用的配置示例:

指定测试包含/排除规则

可以在 <plugin> 标签内添加以下配置来指定要包含或排除的测试:

<configuration>
    <includes>
        <include>**/*Test.java</include>
        <include>**/*Tests.java</include>
        <include>**/*TestCase.java</include>
    </includes>
    <excludes>
        <exclude>**/*IntegrationTest.java</exclude>
    </excludes>
</configuration>
设置系统属性

可以设置系统属性,使这些属性在测试中可用:

<configuration>
    <systemPropertyVariables>
        <propertyName>propertyValue</propertyName>
    </systemPropertyVariables>
</configuration>
并行运行测试

可以通过以下配置并行运行测试以加速测试过程:

<configuration>
    <parallel>methods</parallel>
    <threadCount>4</threadCount>
</configuration>

总结

Maven Surefire 插件是 Maven 项目中运行单元测试的重要工具。

通过使用此插件,可以确保在构建过程中运行单元测试,有助于保持代码质量并及早发现问题。

本指南全面介绍了设置和使用 Maven Surefire 插件的方法,并提供了一个实际示例帮助您入门。

掌握这些知识后,您可以有效地管理和运行 Maven 项目中的单元测试。


网站公告

今日签到

点亮在社区的每一天
去签到