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

如何在TestNg自定义报告中显示测试套件的总用例执行数?

Hey there! I’ve tackled this exact problem when building custom TestNG reports—here’s a straightforward way to get the total number of test cases in your suite and display it in your report:

获取TestNG测试套件总用例数并展示在自定义报告中

方法1:通过IReporter接口(测试结束后统计)

If you’re generating your custom report after all tests finish, the IReporter interface is perfect for this. It lets you pull suite-level data directly from TestNG’s built-in objects:

  1. 实现IReporter接口,重写generateReport方法:
import org.testng.IReporter;
import org.testng.ISuite;
import org.testng.ISuiteResult;
import org.testng.ITestContext;
import org.testng.xml.XmlSuite;

import java.util.List;

public class CustomTestReporter implements IReporter {
    @Override
    public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites, String outputDirectory) {
        int totalTestCases = 0;
        
        // 遍历所有测试套件
        for (ISuite suite : suites) {
            // 遍历套件下的所有测试结果集
            for (ISuiteResult result : suite.getResults().values()) {
                ITestContext testCtx = result.getTestContext();
                // 累加所有测试方法的数量(包含未执行的用例)
                totalTestCases += testCtx.getAllTestMethods().length;
            }
        }
        
        // 这里替换成你已经获取的pass/fail/skip统计值
        int passedCases = ...;
        int failedCases = ...;
        int skippedCases = ...;
        
        // 示例:输出到控制台,实际可以渲染为HTML/JSON等格式的报告内容
        System.out.println("=== 测试套件统计 ===");
        System.out.println("总用例数: " + totalTestCases);
        System.out.println("通过: " + passedCases + " | 失败: " + failedCases + " | 跳过: " + skippedCases);
    }
}
  1. 在TestNG配置文件(testng.xml)中注册这个自定义报告器:
<suite name="YourTestSuite">
    <listeners>
        <listener class-name="com.your.package.CustomTestReporter"/>
    </listeners>
    <!-- 你的测试类/方法配置 -->
</suite>

方法2:通过ISuiteListener(测试执行中实时获取)

If you need the total count while tests are running (not just after they finish), use the ISuiteListener to calculate the number when the suite starts:

import org.testng.ISuite;
import org.testng.ISuiteListener;
import org.testng.ITestContext;

public class SuiteTotalCaseCounter implements ISuiteListener {
    @Override
    public void onStart(ISuite suite) {
        int totalCases = 0;
        // 在套件启动时统计所有测试方法
        for (ITestContext testCtx : suite.getTestContexts()) {
            totalCases += testCtx.getAllTestMethods().length;
        }
        // 将总数存入套件属性,供其他监听器或报告器调用
        suite.setAttribute("TOTAL_TEST_CASES", totalCases);
        System.out.println("当前测试套件总用例数: " + totalCases);
    }

    @Override
    public void onFinish(ISuite suite) {
        // 套件结束时的收尾操作(可选)
    }
}

注册这个监听器到testng.xml后,你可以在自定义报告器里通过suite.getAttribute("TOTAL_TEST_CASES")获取总用例数。

关键提示

  • getAllTestMethods()返回套件中定义的所有@Test标记的方法,包括被跳过、因依赖失败未执行的用例,所以这个数值就是你需要的总用例数。
  • 如果你的测试用例有分组或依赖配置,这个方法依然能准确统计所有标记为测试的方法,不会遗漏。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.29 08:07:35