maven-surefire-plugin 单元测试套件

maven-surefire-plugin 单元测试套件

前言

众所周知,Junit可用于单元测试,maven编译的时候也可以整体执行测试用例,但是很多时候测试用例过多,并且我们不希望老是重复执行已经验证过没有问题的用例而耗费时间,这个时候就要用到测试集了,也就是说通过配置的形式有针对性的对需要测试的用例进行检查!

Junit本身提供了测试套件,但是这个测试集不能生成测试报告,大家可以有选择性的参考下:

https://www.w3cschool.cn/junit/36em1hv1.html

我们今天介绍的是apache maven提供的一个集测试套件和测试报告的插件:maven-surefire-plugin

使用

依赖坐标

<?xml version="1.0" encoding="UTF-8"?>
<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>org.example</groupId>
    <artifactId>test</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.8.2</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0-M5</version>
                <configuration>
                    <includesFile>
                        surefire-setting.xml
                    </includesFile>
                    <!--JDK8及以下不需要配置如下JVM参数-->
                    <argLine>
                        --add-opens java.base/java.lang=ALL-UNNAMED
                        --add-opens java.base/java.math=ALL-UNNAMED
                        --add-opens java.base/java.util=ALL-UNNAMED
                        --add-opens java.base/java.util.concurrent=ALL-UNNAMED
                        --add-opens java.base/java.net=ALL-UNNAMED
                        --add-opens java.base/java.text=ALL-UNNAMED
                    </argLine>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

测试用例

跟普通的Junit测试用例唯一的区别就是@Test需要使用org.junit.jupiter.api.Test才能被插件扫描到

surefire-setting.xml测试集配置

项目根路径编写

<!--可以使用全路径类名,也可以使用表达式,**表示多层目录,*表示一层目录-->
<pre>
    <code>
        */test21/*
        **/Test22*.java
        test1.Test1.java
    </code>
</pre>

这个地方其实任何文件都可以(本人已测试),只要配置的测试集是一行一行的即可,但是看标签源码是如上格式,那就按规矩来!

执行脚本

项目根路径编写surefire.sh,当然,你在根目录下直接执行如下命令也可!

mvn clean surefire-report:report

执行脚本后,可以看到项目target目录下多了两个目录surefire-reports、site,打开site目录下的测试报告文件如下:

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Maven插件是一种可插拔的工具,可以在Maven构建过程中执行特定的任务。其中,maven-dependency-plugin和maven-surefire-plugin是两个常用的插件。 maven-dependency-plugin插件可以用来管理项目依赖,可以帮助我们列出项目中的依赖关系,复制依赖文件到指定目录,解压依赖文件等。常用的配置包括: - list:列出项目依赖 - copy-dependencies:将所有依赖文件复制到指定目录 - unpack:解压指定的依赖文件 maven-surefire-plugin插件则是用来执行项目的单元测试的。它可以在Maven构建过程中自动执行单元测试,并生成测试报告。常用的配置包括: - includes/excludes:指定要执行的测试类或排除的测试类 - parallel:指定测试是否并行执行 - reportsDirectory:指定测试报告生成的目录 在POM文件中配置这两个插件,可以通过以下方式: ``` <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <version>3.2.0</version> <executions> <execution> <id>copy-dependencies</id> <phase>package</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <outputDirectory>${project.build.directory}/lib</outputDirectory> </configuration> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>3.0.0-M5</version> <configuration> <includes> <include>**/*Test.java</include> </includes> <parallel>methods</parallel> <threadCount>10</threadCount> <reportsDirectory>${project.build.directory}/surefire-reports</reportsDirectory> </configuration> </plugin> </plugins> </build> ``` 以上是一个简单的POM文件中Maven插件配置maven-dependency-plugin和maven-surefire-plugin的示例,其中maven-dependency-plugin在package阶段执行复制依赖文件的任务,maven-surefire-plugin在test阶段执行单元测试
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值