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

如何按方法统计JUnit5参数化测试的通过与失败执行次数?

按JUnit5参数化测试方法统计执行结果的方案

1. 自定义JUnit5 TestExecutionListener

基于JUnit5原生API实现监听器,在测试执行过程中按方法名累计通过/失败次数,最后统一打印统计结果,无需额外依赖。

代码示例:

import org.junit.platform.engine.TestExecutionResult;
import org.junit.platform.engine.TestSource;
import org.junit.platform.engine.support.descriptor.MethodSource;
import org.junit.platform.launcher.TestExecutionListener;
import org.junit.platform.launcher.TestIdentifier;
import org.junit.platform.launcher.TestPlan;
import java.util.HashMap;
import java.util.Map;

public class MethodLevelStatsListener implements TestExecutionListener {
    private final Map<String, MethodStats> methodStatsMap = new HashMap<>();

    private static class MethodStats {
        int passed = 0;
        int failed = 0;
    }

    @Override
    public void executionFinished(TestIdentifier testIdentifier, TestExecutionResult testExecutionResult) {
        // 仅统计参数化测试的单个执行实例
        if (testIdentifier.isTest() && testIdentifier.getParent().isPresent()) {
            TestIdentifier parentMethod = testIdentifier.getParent().get();
            TestSource source = parentMethod.getSource().orElse(null);
            if (source instanceof MethodSource) {
                String methodName = ((MethodSource) source).getMethodName();
                MethodStats stats = methodStatsMap.computeIfAbsent(methodName, k -> new MethodStats());
                switch (testExecutionResult.getStatus()) {
                    case SUCCESSFUL:
                        stats.passed++;
                        break;
                    case FAILED:
                        stats.failed++;
                        break;
                    default:
                        // 忽略跳过等状态,可按需调整
                        break;
                }
            }
        }
    }

    @Override
    public void testPlanExecutionFinished(TestPlan testPlan) {
        System.out.println("\n=== 测试方法执行统计 ===");
        methodStatsMap.forEach((methodName, stats) ->
                System.out.printf("%s failed: %d, passed: %d%n", methodName, stats.failed, stats.passed)
        );
    }
}

使用方式:

  • 在src/test/resources/META-INF/services/org.junit.platform.launcher.TestExecutionListener文件中添加该类的全限定名,让JUnit自动加载;
  • 或启动测试时通过JVM参数指定:-Djunit.platform.listeners=com.yourpackage.MethodLevelStatsListener。

2. 用JUnit5 Console Launcher输出详细结果

JUnit5自带的Console Launcher支持按方法分组展示参数化测试的执行统计,通过指定--details参数即可实现:

Maven环境命令示例:

mvn test -Dtest=你的测试类名 -Djunit.platform.console.options.details=tree

执行后控制台会输出树形结构的结果,每个测试方法下会列出所有参数化实例的状态,同时在方法末尾显示该方法的总通过/失败/跳过次数。

3. 集成Allure测试报告工具

Allure会自动对参数化测试按方法分组,生成的报告中每个测试方法会单独展示其下所有用例的执行统计,包括通过、失败、跳过的数量,还可查看每个用例的详细日志。

Maven配置要点:

  • 在pom.xml中添加Allure Maven插件依赖;
  • 执行测试并生成报告:mvn test allure:report;
  • 打开本地报告:mvn allure:serve。

进入报告的测试类详情页,即可看到每个测试方法的独立统计数据。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.07 21:47:08