测试配置与分组执行
maven-surefire-plugin 配置测试执行规则,支持分组和过滤。
测试包含/排除
基本配置
XML
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<configuration>
<includes>
<include>**/*Test.java</include>
<include>**/*Tests.java</include>
</includes>
<excludes>
<exclude>**/*IntegrationTest.java</exclude>
<exclude>**/*SlowTest.java</exclude>
</excludes>
</configuration>
</plugin>
默认包含规则
| 模式 | 说明 |
|---|---|
| **/*Test.java | 结尾为 Test 的类 |
| **/*Tests.java | 结尾为 Tests 的类 |
| **/*TestCase.java | 结尾为 TestCase 的类 |
指定测试类
命令行指定
Bash
mvn test -Dtest=UserServiceTest
mvn test -Dtest=UserServiceTest,OrderServiceTest
mvn test -Dtest=*ServiceTest
指定测试方法
Bash
mvn test -Dtest=UserServiceTest#testCreate
mvn test -Dtest=UserServiceTest#testCreate+testUpdate
测试分组
JUnit 5 分组
Java
import org.junit.jupiter.api.Tag;
@Tag("fast")
class FastTest {
@Test
void test1() {}
}
@Tag("slow")
class SlowTest {
@Test
void test1() {}
}
Maven 配置分组
XML
<configuration>
<groups>fast</groups> <!-- 仅执行 fast 组 -->
<excludedGroups>slow</excludedGroups> <!-- 排除 slow 组 -->
</configuration>
执行分组
Bash
mvn test -Dgroups=fast
mvn test -DexcludedGroups=slow,integration
并行测试
XML
<configuration>
<parallel>classes</parallel> <!-- 类级并行 -->
<threadCount>4</threadCount> <!-- 4线程 -->
<useUnlimitedThreads>false</useUnlimitedThreads>
</configuration>
parallel 值
| 值 | 说明 |
|---|---|
| none | 不并行 |
| methods | 方法级并行 |
| classes | 类级并行 |
| suites | 测试套件并行 |
| both | 类和方法并行 |
JVM 参数
XML
<configuration>
<argLine>-Xmx512m -XX:+HeapDumpOnOutOfMemoryError</argLine>
</configuration>
常用 JVM 参数
| 参数 | 说明 |
|---|---|
| -Xmx | 最大内存 |
| -Xms | 初始内存 |
| -XX:+HeapDumpOnOutOfMemoryError | OOM 时生成 dump |
系统属性
XML
<configuration>
<systemProperties>
<property>
<name>env</name>
<value>test</value>
</property>
<property>
<name>config.path</name>
<value>${project.basedir}/config</value>
</property>
</systemProperties>
</configuration>
测试失败处理
XML
<configuration>
<testFailureIgnore>true</testFailureIgnore> <!-- 失败继续 -->
</configuration>
命令行
Bash
mvn test -Dmaven.test.failure.ignore=true
跳过测试
XML
<configuration>
<skip>true</skip>
</configuration>
命令行
Bash
mvn package -DskipTests
mvn package -Dmaven.test.skip=true
测试输出
重定向输出
XML
<configuration>
<redirectTestOutputToFile>true</redirectTestOutputToFile>
<reportsDirectory>target/test-reports</reportsDirectory>
</configuration>
依赖测试范围
XML
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.8.2</version>
<scope>test</scope>
</dependency>
</dependencies>
JUnit 5 配置
XML
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.8.2</version>
<scope>test</scope>
</dependency>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
</plugin>
TestNG 配置
XML
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.5</version>
<scope>test</scope>
</dependency>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
测试报告
生成报告
Bash
target/surefire-reports/
├── TEST-*.xml
├── *.txt
HTML 报告
text
mvn surefire-report:report
输出:target/site/surefire-report.html
要点总结
- includes/excludes 配置测试类过滤
- -Dtest 指定测试类或方法
- groups/excludedGroups 测试分组
- parallel 并行测试执行
- argLine 配置测试 JVM 参数
- systemProperties 设置测试系统属性
- skipTests 跳过测试执行
- surefire-report:report 生成 HTML 报告
📝 发现内容有误?点击此处直接编辑