温馨提示×

Postman在Ubuntu上如何生成报告

小樊
41
2025-11-22 14:14:30
栏目: 智能运维

在 Ubuntu 上生成 Postman 报告的实用方案

一 准备导出文件与环境

  • 在 Postman 中导出:
    • 集合 Collection:左侧 Collections → 目标集合右侧 … → Export → 选择 v2.1,保存为 .postman_collection.json
    • 环境 Environment:右上角 齿轮 → Manage Environments → 目标环境 → Download,保存为 .postman_environment.json
    • 全局变量 Globals:同页 Globals → Download,保存为 .postman_globals.json
    • 数据文件(可选,用于参数化):准备 data.csvdata.json,首行或键名需与请求中的变量名一致。
  • 在 Ubuntu 安装 Node.jsnpm(示例):
    • 安装 Node.js(建议 Node ≥ 14),验证:node -vnpm -v
  • 安装 Newman(Postman 命令行运行器):
    • 全局安装:npm install -g newman
  • 如需 HTML 报告,安装 HTML 报告插件(见下文各方案)。

二 本地生成报告

  • 方案 A HTML 报告(newman-reporter-html)

    • 安装插件:npm install -g newman-reporter-html
    • 运行示例:
      newman run collection.json \
        -e environment.json \
        -d data.json \
        -r html \
        --reporter-html-export report.html
      
    • 报告将导出为 report.html,可用浏览器打开查看。
  • 方案 B Allure 报告(更美观,适合 CI)

    • 安装 Allure 命令行(示例):
      curl -o- https://dl-cli.pstmn.io/install/linux64.sh | sh
      allure --version
      
    • 运行并导出 Allure 结果:
      newman run collection.json \
        -e environment.json \
        -r allure \
        --reporter-allure-export allure-results
      
    • 生成 HTML 并打开:
      allure generate allure-results -o allure_html --clean
      allure open allure_html
      
    • 如需同时输出控制台与 Allure,可用:-r cli,allure
  • 方案 C 其他报告格式(JSON、JUnit)

    • 同时输出控制台、JSON、JUnit:
      newman run collection.json \
        -e environment.json \
        -d data.json \
        -r cli,json,junit \
        --reporter-json-export result.json \
        --reporter-junit-export result.xml
      
    • 适合接入 Jenkins、GitLab CI 等流水线做质量门禁与报表归档。

三 常见报错与排查

  • 报错:newman: “html” reporter could not be loaded.
    • 原因:未安装 HTML 报告插件。
    • 解决:npm install -g newman-reporter-html,再执行命令。
  • 报错:找不到环境/数据文件
    • 解决:使用绝对路径或确保相对路径正确,例如:newman run /path/collection.json -e /path/env.json -d /path/data.json
  • 报告生成但为空或无断言结果
    • 解决:确认 Collection 的 Tests 脚本中已编写断言(如 pm.test),否则报告无失败/通过统计。

四 在 CI 中使用 Postman CLI 生成报告

  • 在 GitHub Actions 示例(Ubuntu runner):
    name: Run Postman Tests
    on: push
    jobs:
      postman-tests:
        runs-on: ubuntu-latest
        steps:
          - uses: actions/checkout@v4
          - name: Install Postman CLI
            run: curl -o- "https://dl-cli.pstmn.io/install/linux64.sh" | sh
          - name: Login to Postman CLI
            run: postman login --with-api-key ${{ secrets.POSTMAN_API_KEY }}
          - name: Run Collection
            run: |
              postman collection run ${{ secrets.COLLECTION_ID }} \
                --environment ${{ secrets.ENVIRONMENT_ID }} \
                --reporters cli,json,junit \
                --reporter-json-export result.json \
                --reporter-junit-export result.xml
    
  • 说明:Postman CLI 适合在 CI 中直接运行集合;若需 Allure/HTML 报告,仍建议使用 Newman 在 CI 步骤中执行并导出结果。

0