You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Maven仅运行之前执行失败的JUnit 5测试?

仅运行上一次失败的JUnit 5测试(Maven环境)

核心思路

通过自定义TestWatcher收集失败测试的类/方法信息,写入本地文件;再通过Maven插件读取该文件,动态传递给Surefire插件,实现仅运行失败测试的需求。


步骤1:用TestWatcher生成失败测试清单

自定义TestWatcher扩展类,在测试执行完成后,将失败的测试类全限定名(或方法)写入failed-tests.txt文件:

import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.TestWatcher;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashSet;
import java.util.Set;

public class FailedTestTracker implements TestWatcher {
    private final Set<String> failedTests = new HashSet<>();

    @Override
    public void testFailed(ExtensionContext context, Throwable cause) {
        // 收集全限定测试类名,若要精确到方法,格式改为:类名#方法名
        String testIdentifier = context.getTestClass().orElseThrow().getName();
        failedTests.add(testIdentifier);
    }

    @Override
    public void testPlanExecutionFinished(ExtensionContext context) {
        try {
            // 写入文件,覆盖旧内容
            Files.write(Paths.get("failed-tests.txt"), failedTests);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
  • 注册方式:在测试类上添加@ExtendWith(FailedTestTracker.class),或通过src/test/resources/META-INF/services/org.junit.jupiter.api.extension.Extension文件配置全局生效。

步骤2:Maven配置动态读取清单

修改pom.xml,通过properties-maven-plugin读取清单文件,将内容转为Surefire插件支持的逗号分隔格式:

<build>
    <plugins>
        <!-- 读取失败测试清单,生成可被Surefire识别的属性 -->
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>properties-maven-plugin</artifactId>
            <version>1.0.0</version>
            <executions>
                <execution>
                    <id>read-failed-tests</id>
                    <phase>initialize</phase>
                    <goals>
                        <goal>read-project-properties</goal>
                    </goals>
                    <configuration>
                        <files>
                            <file>failed-tests.txt</file>
                        </files>
                        <properties>
                            <property>
                                <name>failedTests</name>
                                <value>${file.content.replace('\n', ',')}</value>
                            </property>
                        </properties>
                    </configuration>
                </execution>
            </executions>
        </plugin>

        <!-- Surefire插件动态指定测试范围 -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>3.2.5</version>
            <configuration>
                <!-- 若failedTests属性存在则只跑失败测试,否则跑全部 -->
                <test>${failedTests:}</test>
            </configuration>
        </plugin>
    </plugins>
</build>

步骤3:执行逻辑

  • 首次运行全量测试:执行mvn test,失败测试类会自动写入failed-tests.txt。
  • 仅运行失败测试:再次执行mvn test,Maven会自动读取清单,只运行标记失败的测试。
  • 重置全量测试:删除failed-tests.txt后,执行mvn test即可重新跑全部测试。

补充说明

  • 若需精确到单个测试方法,修改testFailed方法中的testIdentifier为context.getTestClass().orElseThrow().getName() + "#" + context.getTestMethod().orElseThrow().getName()即可。
  • 将failed-tests.txt加入.gitignore,避免提交到版本库。

内容的提问来源于stack exchange,提问作者Idza

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 08:20:07