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

在Newman中生成含跳过用例的JUnit报告方案咨询

问题场景

我把Postman里的大量测试用例拆分到多个集合中,方便通过Jenkins独立运行。

调用集合的配置

package.json中的Newman执行命令配置如下:

newman run "collections/${npm_config_collection}.postman_collection.json" \
  -e "environments/TEST.postman_environment.json" \
  --reporters junitfull,htmlextra \
  --reporter-junitfull-export "newman/${npm_config_collection}.xml" \
  --reporter-htmlextra-export "newman/${npm_config_collection}.html" \
  --insecure --env-var access_token=$TOKEN

(注:原命令中Authorization参数疑似格式错误,已调整为Newman常规命令格式)

Jenkins结果报告配置

Jenkins流水线的post阶段配置:

post {
  always {
    echo ' * * *  Zipping HTMLExtra results'
    script {
      sh script: "zip -j newman/\$(date +'%Y-%m-%d_%H-%M-%S')_htmlreports.zip newman/*.html"
    }
    echo ' * * *  Publishing results as Zip file(s)'
    archiveArtifacts artifacts: 'newman/*.zip', allowEmptyArchive: true

    echo ' * * *  Publishing JUnit results'
    junit testResults: 'newman/*.xml', skipPublishingChecks: true, allowEmptyResults: true
  }
}

由于部分集合运行耗时较长,我设置了参数控制是否启用这些集合,但跳过集合会导致Jenkins统计的总测试用例数波动(见下图):
Newman测试趋势图

我希望总测试用例/断言数保持恒定,但Newman目前没有直接将整个集合标记为跳过的功能。除了编写脚本读取该集合上一次运行的报告并批量标记用例为跳过,或者给Newman提交PR新增功能外,还有其他方法可以生成符合要求的JUnit XML文件吗?


解决方案

以下是几种无需依赖历史报告或修改Newman源码的可行方案:

1. 预先生成"跳过状态"的JUnit XML模板

针对每个集合,提前生成一份包含该集合所有测试用例、且全部标记为<skipped/>的JUnit XML模板文件。当需要跳过某个集合时,直接将这份模板复制到Jenkins的newman/目录下,替代原本要生成的报告文件:

  • 执行步骤:
    1. 正常运行一次目标集合,得到完整的JUnit XML报告
    2. 用Shell/Python脚本批量将所有<testcase>节点替换为带有<skipped/>的版本,保存为模板(如templates/${collection}_skipped.xml)
    3. 在Jenkins流水线中添加判断逻辑:若当前集合被跳过,执行cp templates/${npm_config_collection}_skipped.xml newman/${npm_config_collection}.xml

2. 动态修改集合后空跑生成跳过报告

给目标集合添加全局前置脚本,通过环境变量控制是否跳过所有请求:

  • 集合级前置脚本:
    if (pm.environment.get("SKIP_COLLECTION") === "true") {
        pm.test("跳过集合测试", function () {
            pm.expect(true).to.be.true;
        });
        pm.execution.skipRequest(); // 跳过当前请求
    }
    
  • 在Jenkins中,当需要跳过集合时,给Newman命令添加--env-var SKIP_COLLECTION=true参数,此时集合会生成包含所有测试用例名称、且标记为跳过的报告(实际不会执行请求)

3. 基于集合元数据动态生成跳过报告

编写脚本(Node.js/Python均可)读取Postman集合JSON文件,解析出所有测试用例名称,按照JUnit XML格式生成文件,所有<testcase>节点添加<skipped/>标签:

  • 在Jenkins流水线中,跳过集合时调用该脚本生成对应的XML文件,放入newman/目录即可

4. 临时修改集合后运行生成报告

通过工具临时修改集合,添加跳过逻辑后运行:

# 导出原集合并给所有请求添加前置跳过脚本
jq '.item[] |= . + {event: [{listen: "prerequest", script: {exec: ["pm.execution.skipRequest();"]}}]}' collections/${npm_config_collection}.postman_collection.json > temp_skipped_collection.json
# 运行修改后的集合生成跳过报告
newman run temp_skipped_collection.json \
  -e environments/TEST.postman_environment.json \
  --reporters junitfull \
  --reporter-junitfull-export newman/${npm_config_collection}.xml \
  --insecure
# 删除临时文件
rm temp_skipped_collection.json

这种方法无需修改原集合,通过临时生成带跳过逻辑的集合文件运行,生成的报告会包含所有原用例并标记为跳过


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.15 13:26:33