温馨提示×

Ubuntu Postman如何使用脚本功能

小樊
42
2025-12-29 22:39:51
栏目: 智能运维

Ubuntu 下 Postman 脚本功能使用指南

一 环境准备与安装

  • Ubuntu 上可用多种方式安装 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 64 位包:下载解压并创建软链
      • 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
  • 安装完成后在终端输入 postman 启动应用。

二 脚本类型与执行顺序

  • Postman 脚本基于 JavaScript,在请求生命周期的两个关键点执行:
    • Pre-request Script:请求发送前执行,常用于设置 HeadersQuery认证 Token、动态时间戳/签名等。
    • Tests:收到响应后执行,用于断言与结果校验(状态码、响应时间、响应体字段等)。
  • 脚本作用域与执行顺序(由外到内,先后执行):
    • 集合级脚本 → 文件夹级脚本 → 请求级脚本;对应地,测试脚本在请求后按相反顺序收尾。
    • 同一层级内:先执行所有前置脚本,再执行所有测试脚本。
  • 常用变量作用域:EnvironmentGlobalCollection VariablesData(数据文件)Local

三 编写与调试脚本

  • 基本用法示例
    • Pre-request Script(动态注入 Authorization 与查询参数)
      • const token = pm.environment.get(“token”) || “”;
      • const userId = pm.collectionVariables.get(“userId”);
      • pm.request.headers.add({ key: “Authorization”, value: "Bearer " + token });
      • if (userId) { pm.request.url.query.add({ key: “userId”, value: userId }); }
      • console.log(“Pre-request: token=”, token, “userId=”, userId);
    • Tests(状态码、响应时间、JSON 字段与类型校验)
      • pm.test(“Status code is 200”, () => { pm.response.to.have.status(200); });
      • pm.test(“Response time < 200ms”, () => { pm.expect(pm.response.responseTime).to.be.below(200); });
      • pm.test(“Response has userId and is string”, () => {
        • const json = pm.response.json();
        • pm.expect(json).to.have.property(“userId”).that.is.a(“string”);
        • });
  • 变量与数据驱动
    • 读取/设置变量:pm.environment.get/set、pm.collectionVariables.get/set、pm.globals.get/set。
    • 在请求中使用 {{variable}} 引用变量;数据文件(CSV/JSON)通过 Collection Runner 的 Data 选项上传,脚本中用 pm.iterationData.get(“列名”) 读取。
  • 调试与日志
    • 使用 console.log 输出关键变量与分支走向;在桌面客户端的 Console/测试输出面板 查看脚本日志与断言结果。

四 运行与自动化

  • 在 Postman 桌面客户端
    • 选中集合,使用 Collection Runner 批量运行;选择 Environment、设置 Iterations、上传 Data File,即可执行集合内全部请求的脚本并查看报告。
  • 命令行与 CI/CD(Newman)
    • 安装 Newman:npm install -g newman
    • 基本运行:newman run collection.json -e environment.json
    • 生成报告:newman run api_collection.json -e Test_Env.json --reporters cli,json --reporter-json-export newman_report.json
    • Jenkins/GitHub Actions/GitLab CI 中集成 Newman,实现自动化回归与定时巡检。

五 进阶技巧与最佳实践

  • 流程控制:在同一集合/文件夹内使用 postman.setNextRequest(“Request Name”)postman.setNextRequest(null) 控制请求链路与跳过逻辑。
  • 作用域与复用:将 base_url、token、userId 等放入 Environment/Collection Variables,脚本尽量原子化,单个 pm.test 只验证一个明确结论,便于报告阅读与维护。
  • 性能阈值:为关键接口设置 响应时间阈值 断言;在 Pre-request 中集中处理 Token/签名/时间戳,避免在 Tests 中做耗时操作。
  • 报告与可视化:Newman 支持 cli/json/html 等报告,结合 newman-reporter-htmlextra 生成更友好的 HTML 报告。

0