温馨提示×

Postman Ubuntu脚本编写指南

小樊
32
2026-01-02 20:13:42
栏目: 智能运维

Postman Ubuntu 脚本编写指南

一 环境准备与安装

  • Ubuntu 上可使用多种方式安装 Postman:
    • Snap:sudo snap install postman --classic
    • 官方 .deb 包: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
    • APT 仓库(可选):导入 GPG 并添加源后 sudo apt install postman
  • 安装完成后可直接运行命令:postman 启动桌面应用。

二 脚本类型与核心 API

  • 脚本类型与执行时机
    • Pre-request Script:在请求发送前执行,常用于设置 HeadersQuery认证 Token、生成时间戳/签名等。
    • Tests:在收到响应后执行,用于断言 状态码响应头响应体响应时间 等。
  • 常用对象与方法
    • pm.environment:get/set 环境变量(如 {{baseUrl}}、{{token}})
    • pm.collectionVariables:集合级变量,适合在多个请求间共享数据
    • pm.response:status、headers、responseTime、json() 等
    • pm.test / pm.expect:编写可读的断言
    • console.log:输出到 Postman 控制台,便于调试
  • 快速示例
    • Pre-request
      • console.log(“Pre-request running”);
      • const token = pm.environment.get(“token”);
      • pm.request.headers.add({ key: “Authorization”, value: "Bearer " + token });
    • Tests
      • pm.test(“Status code is 200”, () => pm.response.to.have.status(200));
      • pm.test(“Response time < 500ms”, () => pm.expect(pm.response.responseTime).to.be.below(500));
      • const json = pm.response.json();
      • pm.test(“Has id field”, () => pm.expect(json).to.have.property(“id”));
      • pm.collectionVariables.set(“userId”, json.id); // 供后续请求使用

三 数据与变量管理

  • 变量层级与优先级
    • 集合变量(Collection Variables):同一集合内共享,适合提取一次、多处使用(如 userId
    • 环境变量(Environment Variables):区分 开发/测试/生产 等环境(如 baseUrl
    • 全局变量(Globals):对所有环境可见
    • 引用方式:在 URL/Headers/Body 中使用 {{变量名}}
  • 数据驱动与迭代
    • Collection Runner 中可使用 数据文件(CSV/JSON) 进行参数化,多组输入批量运行
    • 结合 IterationsDelay 控制并发与节奏
  • 示例:从响应提取并共享
    • const userId = pm.response.json().id;
    • pm.collectionVariables.set(“userId”, userId); // 后续请求用 {{userId}} 引用

四 命令行自动化与 CI 集成

  • 使用 Newman(Postman 命令行工具)在 Ubuntu 无头运行集合
    • 安装:npm install -g newman
    • 运行:newman run collection.json --environment environment.json
    • 报告:newman run collection.json --reporters cli,junit --reporter-junit-export report.xml
  • 示例 GitHub Actions 片段
    • name: Run Postman Tests on: push jobs: api-tests: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - uses: actions/setup-node@v4 with: { node-version: ‘18’ } - run: npm install -g newman - run: newman run “your_collection.json” --reporters cli,junit --reporter-junit-export report.xml - uses: actions/upload-artifact@v4 with: { name: postman-report, path: report.xml }
  • 适用场景:本地 Collection Runner、服务器定时任务、Docker 容器、CI/CD 流水线。

五 调试技巧与最佳实践

  • 调试
    • 使用 console.log 输出关键变量与流程节点;在 Postman 底部 Console 查看日志与错误堆栈
    • Tests 面板查看每个断言的通过/失败与失败原因
  • 可维护性
    • 将通用逻辑封装为 集合级 Pre-request/Tests 脚本,减少重复
    • 统一 命名规范(变量、集合、请求),用 文件夹 管理业务域
    • 合理使用 环境变量/集合变量 管理配置与运行时数据
  • 进阶
    • 对复杂响应结构使用 JSON Schema 校验(如 tv4),提升契约一致性验证能力
  • 常见问题
    • 变量未生效:检查变量作用域与拼写,确认已选中正确的 Environment
    • 认证失败:确认 Authorization 头是否正确设置,Token 是否过期
    • 脚本报错:打开 Console 定位行号与异常信息,逐步打印中间变量

0