首页主机资讯Postman在Ubuntu中的集成测试如何进行

Postman在Ubuntu中的集成测试如何进行

时间2025-12-08 18:30:05发布访客分类主机资讯浏览1219
导读:在 Ubuntu 中进行 Postman 集成测试 一 环境准备与安装 安装 Postman(任选其一) Snap:sudo snap install postman --classic APT:导入 GPG 并添加仓库后安装 wge...

在 Ubuntu 中进行 Postman 集成测试

一 环境准备与安装

  • 安装 Postman(任选其一)
    • Snap:sudo snap install postman --classic
    • APT:导入 GPG 并添加仓库后安装
      • wget -qO - https://dl.postman.co/postman.gpg | sudo apt-key add -
      • echo “deb https://dl.postman.co/debian $(lsb_release -cs) main” | sudo tee /etc/apt/sources.list.d/postman.list
      • sudo apt update & & sudo apt install postman
    • 手动安装:下载 Linux 包、解压至 /opt、创建软链并配置桌面启动器
      • wget https://dl.pstmn.io/download/latest/linux64 -O postman.tar.gz
      • sudo tar -xzf postman.tar.gz -C /opt/
      • sudo ln -s /opt/Postman/Postman /usr/local/bin/postman
      • 创建 /usr/share/applications/postman.desktop(Exec 指向 /opt/Postman/Postman/Postman,Icon 指向对应图标)
  • 安装 Newman(Postman 命令行运行器)
    • npm install -g newman
  • 建议将集合与环境导出到项目仓库,便于版本化与 CI 执行:
    • 集合:collection.json
    • 环境:environment.json(可选)
    • 全局/数据文件:globals.json、data.json(可选)

二 在 Postman 中编写与本地运行

  • 创建请求与集合
    • 新建请求(GET/POST 等),设置 URL、Headers、Body、Query Params
    • 将相关请求组织到 Collection,便于批量运行与共享
  • 配置环境与变量
    • 在 Postman 中创建 Environment,定义如 { { baseUrl} } { { token} } 等变量,便于多环境切换
  • 编写测试脚本(Tests 标签)
    • 示例:
      • 验证状态码:pm.test(“Status code is 200”, () => pm.response.to.have.status(200));
      • 验证响应体字段:pm.test(“Name is correct”, () => pm.expect(pm.response.json().name).to.eql(“test_user”));
  • 本地运行与调试
    • 使用 Runner 选择集合与环境,配置迭代次数、延迟、数据文件后执行
    • Console 查看输出与脚本错误,迭代修正用例与脚本

三 在 Ubuntu 服务器或 CI 中运行 Newman

  • 基本执行
    • 运行集合:newman run collection.json
    • 使用环境:newman run collection.json --environment environment.json
    • 使用数据文件与全局变量:newman run collection.json --environment environment.json --data data.json --globals globals.json
  • 报告与产出
    • 控制台与 JUnit 报告:newman run collection.json --reporters cli,junit --reporter-junit-export report.xml
    • 生成 HTML 报告(需 newman-reporter-html):npm i -D newman-reporter-html,然后
      • newman run collection.json --reporters cli,html --reporter-html-export report.html
  • 无头与静默
    • 在 CI 中建议添加 –silent–no-color 减少日志噪声

四 集成到 CI/CD 示例 GitHub Actions

  • 工作流要点
    • 触发时机:push、pull_request 到 main
    • 运行环境:ubuntu-latest
    • 步骤:安装 Node.js → 安装 Newman → 运行集合并导出 JUnit 报告 → 上传报告产物
  • 示例 postman.yml
    • name: Run Postman API Tests on: push: branches: [ main ] pull_request: branches: [ main ] jobs: postman-tests: runs-on: ubuntu-latest strategy: matrix: node-version: [18.x] steps: - uses: actions/checkout@v3 - name: Setup Node.js uses: actions/setup-node@v3 with: node-version: ${ { matrix.node-version } } - name: Install Newman run: npm install -g newman - name: Run Postman Collection run: | newman run collection.json
      –environment environment.json
      –reporters cli,junit
      –reporter-junit-export report.xml - name: Upload Test Results uses: actions/upload-artifact@v2 with: name: postman-test-results path: report.xml
  • 其他 CI(如 Jenkins)
    • 在构建步骤中执行与上文类似的 Newman 命令,并使用 JUnit 插件 展示报告或 HTML Publisher 发布可视化报告

五 常见问题与最佳实践

  • 认证与敏感信息
    • 避免在集合/脚本中硬编码 token、密码;使用 EnvironmentSecrets 管理,CI 中通过变量注入
  • 环境与数据驱动
    • Environment 区分 dev/staging/prod;用 Data File 做数据驱动测试,覆盖多组输入
  • 稳定性与可维护性
    • 合理使用 setTimeout/retry 处理偶发抖动;为接口契约与关键路径补充 Smoke/Regression 用例
  • 报告与通知
    • 在 CI 中统一产出 JUnit/HTML 报告并归档;失败时结合 GitHub Checks/PR 状态邮件/IM 通知 及时反馈
  • 版本化与协作
    • collection.json、environment.json、globals.json、data.json 纳入 Git;使用 Collection Runner/Monitor 做定时回归

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!


若转载请注明出处: Postman在Ubuntu中的集成测试如何进行
本文地址: https://pptw.com/jishu/766180.html
如何在Ubuntu上利用Postman进行性能测试 Ubuntu上Postman的脚本编写技巧有哪些

游客 回复需填写必要信息