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

如何在Jenkins日志中对测试用例失败情况进行分类统计?

按异常类型分类统计自动化测试失败用例

完全可以实现失败用例按异常类型分类汇总,以下是几个适配Jenkins Pipeline场景的实用方案:

方案1:在测试框架中标记异常类型

如果你的自动化测试基于JUnit、TestNG这类主流框架,可以在测试代码中主动捕获异常并添加自定义标记,让报告直接包含异常分类信息:

  • TestNG示例:在测试方法中捕获异常后,通过ITestContext将异常类型作为测试属性存入结果:
@Test
public void testLogin() {
    try {
        // 测试逻辑
    } catch (NoSuchElementException e) {
        ITestContext context = Reporter.getCurrentTestResult().getTestContext();
        context.setAttribute("failureType", "Element Not Found Exception");
        throw e; // 抛出异常保证测试标记为失败
    } catch (TimeoutException e) {
        ITestContext context = Reporter.getCurrentTestResult().getTestContext();
        context.setAttribute("failureType", "Timeout Exception");
        throw e;
    }
}

生成的TestNG报告中会包含failureType属性,后续可直接基于此统计。

  • JUnit示例:使用@Rule自定义测试监听器,捕获异常并添加分类标签:
public class FailureTypeRule implements TestRule {
    @Override
    public Statement apply(Statement base, Description description) {
        return new Statement() {
            @Override
            public void evaluate() throws Throwable {
                try {
                    base.evaluate();
                } catch (NoSuchElementException e) {
                    Description newDesc = Description.createTestDescription(
                        description.getTestClass(),
                        description.getDisplayName() + " [failureType:Element Not Found Exception]"
                    );
                    throw new AssertionError(newDesc.getDisplayName(), e);
                }
            }
        };
    }
}

// 在测试类中使用
public class LoginTest {
    @Rule
    public FailureTypeRule failureTypeRule = new FailureTypeRule();

    @Test
    public void testLogin() {
        // 测试逻辑
    }
}

方案2:在Jenkins Pipeline中解析测试报告统计

如果不想修改测试代码,可以直接在Pipeline脚本中解析测试框架生成的标准报告(如JUnit XML),提取异常类型并统计:
以下是Groovy脚本示例,用于解析JUnit XML并输出异常分类汇总:

pipeline {
    agent any
    stages {
        stage('Run Tests') {
            steps {
                // 运行自动化测试,生成JUnit报告到target/surefire-reports目录
                sh 'mvn test'
            }
        }
        stage('Analyze Failures') {
            steps {
                script {
                    def failureStats = [:]
                    // 遍历所有JUnit XML报告文件
                    new File('target/surefire-reports').eachFileMatch(~/.*\.xml/) { file ->
                        def xml = new XmlSlurper().parse(file)
                        // 遍历所有失败的测试用例
                        xml.testcase.each { testcase ->
                            if (testcase.failure || testcase.error) {
                                def exceptionType = testcase.failure?.@type ?: testcase.error?.@type
                                if (exceptionType) {
                                    // 统计异常类型出现次数
                                    failureStats[exceptionType] = (failureStats[exceptionType] ?: 0) + 1
                                } else {
                                    // 处理无明确类型的异常
                                    failureStats['Unknown Exception'] = (failureStats['Unknown Exception'] ?: 0) + 1
                                }
                            }
                        }
                    }
                    // 输出统计结果
                    echo "=== 失败用例异常分类汇总 ==="
                    failureStats.each { type, count ->
                        echo "${count}个失败源于${type}"
                    }
                }
            }
        }
    }
}

这段脚本会在测试完成后,自动解析JUnit报告,统计各异常类型的失败数量并打印到Jenkins控制台。

方案3:使用Jenkins插件简化统计

如果不想编写脚本,可以借助Jenkins插件实现可视化的异常分类统计:

  • Test Results Analyzer Plugin:安装后可直接在Jenkins测试结果页面按异常类型、测试套件、用例名称等维度筛选和统计失败用例,支持生成柱状图展示各异常类型的占比。
  • JUnit Plugin(增强配置):在Jenkins项目配置的「Publish JUnit test result report」中,可通过「Test result trend」查看长期的异常类型分布,也支持自定义筛选规则定位特定异常的失败用例。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.14 19:57:57