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

如何用Python生成JSON报告供Jenkins Cucumber插件生成HTML报告?

用Python生成Cucumber兼容JSON报告,在Jenkins中可视化测试结果

完全可以通过Python生成符合Cucumber标准的JSON报告,再交给Jenkins的Cucumber报告插件生成可视化HTML报告,下面是具体的学习要点和操作步骤:

一、需要掌握的核心内容

  • Cucumber JSON报告结构:熟悉Cucumber官方定义的JSON字段,比如feature(测试套件)、scenario(测试用例)、step(测试步骤)、status(结果状态:passed/failed/skipped)、duration(耗时,单位纳秒)等核心字段的含义和层级关系
  • Python JSON处理:会使用内置的json模块构建JSON数据结构,并写入文件
  • Jenkins Cucumber插件配置:了解如何在Jenkins构建后操作中指定JSON报告的路径,让插件识别并解析文件

二、具体操作步骤

1. 对齐Cucumber JSON标准格式

先明确Cucumber JSON的基础结构(简化版),你的Python脚本需要生成符合这个结构的文件:

{
  "features": [
    {
      "name": "回归测试总套件",
      "scenarios": [
        {
          "name": "测试用例名称",
          "steps": [
            {
              "name": "步骤描述",
              "result": {
                "status": "passed",
                "duration": 120000000,
                "error_message": ""
              }
            }
          ]
        }
      ]
    }
  ]
}

注意:duration需要转换为纳秒(1毫秒=1,000,000纳秒);status只能是passed/failed/skipped三个值之一;失败的用例要把错误信息填入error_message。

2. 在Python脚本中生成JSON报告

结合你的测试流程,在脚本中收集每个测试用例的结果,组装成Cucumber格式的JSON数据,最后写入文件:

import json
import time

def run_test_case(test_case):
    # 模拟你的测试逻辑:发送请求、获取结果
    start_time = time.time()
    # 替换成实际的请求处理代码
    response_status = test_case.get("expected_status")
    test_result = "passed" if response_status == 200 else "failed"
    duration_ms = int((time.time() - start_time) * 1000)
    
    return {
        "name": test_case["case_name"],
        "status": test_result,
        "duration_ms": duration_ms,
        "error": "服务器返回非200状态码" if test_result == "failed" else ""
    }

# 模拟从GitHub读取测试用例(替换成实际的拉取/读取逻辑)
github_test_cases = [
    {"case_name": "用户登录接口验证", "expected_status": 200},
    {"case_name": "商品列表接口分页测试", "expected_status": 500}
]

# 构建Cucumber格式的报告数据
cucumber_data = {
    "features": [
        {
            "name": "Web接口回归测试",
            "scenarios": []
        }
    ]
}

for case in github_test_cases:
    test_result = run_test_case(case)
    # 把单个测试用例转换为Cucumber的scenario结构
    scenario = {
        "name": test_result["name"],
        "steps": [
            {
                "name": f"执行接口请求: {case['case_name']}",
                "result": {
                    "status": test_result["status"],
                    "duration": test_result["duration_ms"] * 1000000,
                    "error_message": test_result["error"]
                }
            }
        ]
    }
    cucumber_data["features"][0]["scenarios"].append(scenario)

# 将报告写入工作空间内的指定路径(确保Jenkins能访问到)
with open("./reports/cucumber-test-result.json", "w", encoding="utf-8") as f:
    json.dump(cucumber_data, f, indent=2, ensure_ascii=False)

3. 配置Jenkins任务

  • 构建步骤:添加"执行shell"或"执行Windows批处理命令",运行你的Python脚本(比如python3 test_script.py),确保脚本在Jenkins工作空间内执行,生成的reports文件夹在工作空间根目录下
  • 构建后操作:添加"Publish Cucumber Reports",在"JSON Report Path"中填写JSON文件的路径,比如**/reports/cucumber-test-result.json(**表示递归查找),其他配置可按需调整(比如报告标题、失败用例高亮等)

4. 验证结果

运行Jenkins任务,任务完成后在构建详情页点击"Cucumber Reports"链接,就能看到生成的可视化HTML报告,包含测试用例通过率、失败详情、耗时统计等信息。

可选优化

如果你的测试用例有多个步骤(比如数据准备、请求发送、结果验证),可以把每个步骤拆分为Cucumber的step,让报告更细致;也可以使用behave库(Python版Cucumber),它能自动生成标准Cucumber报告,无需手动拼接JSON,但需要按Gherkin语法编写测试用例。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.02 20:05:30