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

为内部CI工具生成Karate执行结果JSON汇总文件需求

生成Karate执行统计JSON文件用于CI工具

实现步骤

  • 步骤1:编写结果汇总脚本
    在项目根目录创建脚本(根据构建工具选JS/Groovy等),用于遍历Karate测试报告并合并统计值。示例Node.js脚本:

    const fs = require('fs');
    const path = require('path');
    
    // 指向Karate默认报告目录
    const reportDir = path.join(__dirname, 'target', 'karate-reports');
    let passed = 0;
    let failed = 0;
    
    // 遍历所有JSON格式报告
    fs.readdirSync(reportDir).forEach(file => {
      if (file.endsWith('.json')) {
        const report = JSON.parse(fs.readFileSync(path.join(reportDir, file), 'utf8'));
        passed += report.passed || 0;
        failed += report.failed || 0;
      }
    });
    
    // 生成CI所需的JSON文件
    const ciResult = { passed, failed };
    fs.writeFileSync(path.join(__dirname, 'karate-ci-results.json'), JSON.stringify(ciResult, null, 2));
    
  • 步骤2:集成到CI流程
    在CI配置文件(GitHub Actions/GitLab CI/Jenkinsfile等)中,添加测试后执行脚本的步骤。示例GitHub Actions片段:

    - name: 生成Karate CI统计文件
      run: node generate-ci-report.js
    
  • 步骤3:确认输出结果
    执行完成后,项目根目录会生成karate-ci-results.json,内容格式如下:

    {
      "passed": 15,
      "failed": 8
    }
    

注意事项

  • 确保Karate已启用JSON报告生成(默认开启,如需配置可在karate-config.js中添加karate.configure('report', { json: true }))。
  • 根据项目实际结构调整脚本中的报告目录路径,避免找不到文件。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.20 20:08:12