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

Extent Report 4.0.9版本多自动化并行测试报告合并问题求助

合并Extent Report 4.0.9报告失败问题解决

我运行多个并行自动化套件生成了多份Extent Report,想通过代码读取2份现有报告合并成一份整合报告,但用以下代码后合并结果不正确——仅把第二份报告内容追加到第一份,测试结果没有正确合并(异常表现为报告结构混乱、统计信息未整合)。使用的是Extent Report 4.0.9版本。

错误代码示例:

import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.reporter.ExtentHtmlReporter;
import com.aventstack.extentreports.reporter.configuration.Protocol;
import com.aventstack.extentreports.reporter.configuration.Theme;

public class ExtentReportMerger1 {
     public static void main(String[] args) {
     String file1Path = "D:\\CodeAutomation15\\ui-automation-codebase\\logs\\Reports\\ExtentReports1.html";
     String file2Path = "D:\\CodeAutomation15\\ui-automation-codebase\\logs\\Reports\\ExtentReports2.html";

     // Load the first report
     ExtentReports extentReports = new ExtentReports();
     ExtentHtmlReporter htmlReporter1 = new ExtentHtmlReporter(file1Path);
     extentReports.attachReporter(htmlReporter1);

     // Load the second report
     ExtentHtmlReporter htmlReporter2 = new ExtentHtmlReporter(file2Path);

     // Append the details from the second report to the first report
     extentReports.attachReporter(htmlReporter2);

     htmlReporter1.config().setTheme(Theme.STANDARD);
     htmlReporter1.config().setDocumentTitle("Merged Report");
     htmlReporter1.config().setEncoding("UTF-8");
     htmlReporter1.config().setProtocol(Protocol.HTTPS);

     // Flush the reports
     extentReports.flush();

     System.out.println("Merged Extent reports successfully!");
 }
}

问题根源

ExtentHtmlReporter的作用是生成新的HTML报告,而非读取解析已有的HTML报告。直接attach两个指向已有报告的ExtentHtmlReporter,只会对文件内容进行覆盖或追加,无法解析原有报告中的测试数据、统计信息进行逻辑合并。Extent Report 4.x本身不支持直接读取HTML报告进行合并,必须依赖原始测试数据的导出与导入。

正确解决方案

Extent Report 4.x支持通过JSON格式的测试数据文件实现报告合并,分为两步:

1. 修改自动化套件,生成报告时同步导出JSON数据

在每个生成Extent Report的自动化代码中,添加JSON导出器,保存原始测试数据:

import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.reporter.ExtentHtmlReporter;
import com.aventstack.extentreports.reporter.ExtentJsonReporter;

public class GenerateReportWithJson {
    public static void main(String[] args) {
        ExtentReports extent = new ExtentReports();
        // HTML报告生成器
        ExtentHtmlReporter htmlReporter = new ExtentHtmlReporter("single-report.html");
        // JSON数据导出器,用于后续合并
        ExtentJsonReporter jsonReporter = new ExtentJsonReporter("test-data.json");
        
        extent.attachReporter(htmlReporter, jsonReporter);
        
        // 执行测试用例、添加测试步骤等逻辑...
        
        extent.flush();
    }
}

2. 编写合并代码,导入JSON数据生成整合报告

通过loadJSONConfig方法导入多份JSON测试数据,生成合并后的完整报告:

import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.reporter.ExtentHtmlReporter;
import com.aventstack.extentreports.reporter.configuration.Theme;

public class ExtentReportMerger {
    public static void main(String[] args) {
        // 待合并的JSON数据文件路径
        String jsonPath1 = "report1-data.json";
        String jsonPath2 = "report2-data.json";
        // 合并后报告的输出路径
        String mergedReportPath = "merged-test-report.html";

        ExtentReports extent = new ExtentReports();
        ExtentHtmlReporter htmlReporter = new ExtentHtmlReporter(mergedReportPath);
        
        // 配置合并报告的样式与信息
        htmlReporter.config().setTheme(Theme.STANDARD);
        htmlReporter.config().setDocumentTitle("整合自动化测试报告");
        htmlReporter.config().setEncoding("UTF-8");
        extent.attachReporter(htmlReporter);

        // 导入多份JSON测试数据,自动累加合并
        extent.loadJSONConfig(jsonPath1);
        extent.loadJSONConfig(jsonPath2);

        // 生成最终合并报告
        extent.flush();
        System.out.println("报告合并完成!");
    }
}

注意事项

  • 必须确保每个自动化套件都导出JSON数据,HTML报告无法直接被解析合并
  • loadJSONConfig方法会自动累加导入的测试数据,最终生成包含正确统计信息、测试结构的整合报告
  • 如果无法修改原有自动化代码生成JSON,可考虑基于HTML解析的第三方脚本,但稳定性远不如官方的JSON导入方案

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.21 22:23:27