Testng(四):ExtentReports定制报告

testng自带的report简直不忍直视,不仅不好看,子文件还很多

Testng(四):ExtentReports定制报告

于是我们迫切需要一款高大上的report模板工具,ExtentReports正是其中一个

先来看看它的效果,既有单条用例的执行详情,也有图表统计,而且结果只有一个html文件,便于邮件发送

Testng(四):ExtentReports定制报告

以下示例参考自官网,略有微调

public class ExtentReportsListener implements IReporter {

    private final String OUT_PUT = "test-output/";
    private final String FILE_NAME = "extend_report.html";

    private ExtentReports extent;

    private void init() {
        ExtentHtmlReporter htmlReporter = new ExtentHtmlReporter(OUT_PUT + FILE_NAME);
        htmlReporter.config().setDocumentTitle("ExtentReports - Created by TestNG Listener");
        htmlReporter.config().setReportName("ExtentReports - Created by TestNG Listener");
        htmlReporter.config().setTestViewChartLocation(ChartLocation.BOTTOM);
        htmlReporter.config().setTheme(Theme.STANDARD);
        htmlReporter.config().setResourceCDN(ResourceCDN.EXTENTREPORTS);

        extent = new ExtentReports();
        extent.attachReporter(htmlReporter);
    }

    @Override
    public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites, String outputDirectory) {
        init();

        suites.forEach(iSuite -> {
            Map<String, ISuiteResult> result = iSuite.getResults();

            result.values().forEach(iSuiteResult -> {
                ITestContext context = iSuiteResult.getTestContext();

                buildTestNodes(context.getFailedTests(), Status.FAIL);
                buildTestNodes(context.getSkippedTests(), Status.SKIP);
                buildTestNodes(context.getPassedTests(), Status.PASS);
            });
        });

        Reporter.getOutput().forEach(out ->
                extent.setTestRunnerOutput(out)
        );

        extent.flush();
    }

    private void buildTestNodes(IResultMap tests, Status status) {
        ExtentTest test;

        if (tests.size() > 0) {
            for (ITestResult result : tests.getAllResults()) {
                test = extent.createTest(result.getMethod().getMethodName());

                for (String group : result.getMethod().getGroups())
                    test.assignCategory(group);

                if (result.getThrowable() != null) {
                    test.log(status, result.getThrowable());
                }
                else {
                    test.log(status, "Test " + status.toString().toLowerCase() + "ed");
                }

                test.getModel().setStartTime(getTime(result.getStartMillis()));
                test.getModel().setEndTime(getTime(result.getEndMillis()));
            }
        }
    }

    private Date getTime(long millis) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(millis);
        return calendar.getTime();
    }
}

注意:

  • 如果不加htmlReporter.config().setResourceCDN(ResourceCDN.EXTENTREPORTS),报告中引用的css/js文件将来自于https://cdn.rawgit.com,而此地址需要*才能访问;配置成ResourceCDN.EXTENTREPORTS,则无此顾虑
上一篇:Multi-Projector Based Display Code ---- Download


下一篇:Multiprocessing | 进程是什么