Ubuntu下Postman脚本使用指南
一 环境准备与安装
- 使用 Snap(推荐):sudo snap install postman --classic,安装后直接运行命令:postman。
- 使用 APT 官方源:
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
以上方式均可在 Ubuntu 上稳定运行 Postman 桌面客户端。
二 脚本类型与执行顺序
- 脚本类型
- Pre-request Script:请求发送前执行,常用于设置 Headers、Query、认证 Token、动态生成 时间戳/签名 等。
- Tests:响应返回后执行,用于断言与结果校验,如 状态码、响应时间、响应体字段 等。
- 变量作用域
- Environment、Global、Collection Variables、Data(数据文件)、Local。
- 取值示例:pm.environment.get(“token”)、pm.collectionVariables.get(“userId”)。
- 执行顺序(由外到内、先前后测)
- 集合级 Pre → 文件夹级 Pre → 请求级 Pre → 发送请求 → 集合级 Test → 文件夹级 Test → 请求级 Test。
- 调试与日志
- 使用 console.log 输出调试信息;在桌面客户端通过 View → Show Postman Console(Alt+Ctrl+C) 查看日志与错误堆栈。
三 常用脚本示例
- 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);
});
// JSON 结构与字段
pm.test("Response has userId and is string", () => {
const json = pm.response.json();
pm.expect(json).to.have.property("userId").that.is.a("string");
});
- 数据驱动(CSV/JSON):在 Collection Runner 或 Newman 中使用数据文件,脚本通过 pm.iterationData.get(“列名”) 读取当前行数据。
- 常见内置能力
- 发送请求:pm.sendRequest(url, (err, res) => { … })
- 时间戳:Date.now() / 1000
- 哈希:CryptoJS.MD5(“str”).toString()
- 响应解析:pm.response.json()、pm.response.text()、postman.getResponseHeader(“Content-Type”)
以上示例覆盖了 Pre-request、Tests、数据驱动 与 常用内置库 的典型用法。
四 运行与自动化
- 在桌面客户端
- 选择或创建 Collection,在请求中编写 Pre-request Script 与 Tests;点击 Send 发送请求,查看 Tests 结果与 Console 日志。
- 使用 Collection Runner 批量运行、设置 Iterations 与 Data File(CSV/JSON)。
- 使用 Newman 命令行
- 安装:npm install -g newman
- 运行:newman run collection.json -e environment.json --reporters cli,html --reporter-html-export report.html
- 适合在 Jenkins/GitHub Actions/GitLab CI 中做自动化回归与定时巡检。
- CI/CD 示例(GitHub Actions)
name: Postman Automation Test
on: [push, pull_request]
jobs:
run-tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Install Newman
run: npm install -g newman
- name: Run Postman Collection
run: newman run ./collections/api-tests.json -e ./environments/test-env.json --reporters cli,html
上述流程支持本地 Runner 与 CI/CD 的批量执行与报告生成。
五 最佳实践与排错
- 变量与配置管理:将 token、base_url、userId 等放入 Environment/Collection Variables,避免硬编码,便于多环境复用。
- 断言粒度:采用 pm.test + pm.expect,单个测试用例只验证一个明确结论,报告更清晰、易维护。
- 日志与问题定位:合理使用 console.log 输出关键变量与分支走向;在 Postman Console 查看输出与堆栈。
- 性能与稳定:在 Pre-request 中集中处理 Token/签名/时间戳,为关键接口设置 响应时间阈值 断言。
- 持续集成:将 Collection 与 Environment 导出为 JSON,在 CI 中用 Newman 运行并收集结果,结合 HTML/Allure 等报告插件生成可视化报告。
这些实践有助于提升脚本可维护性与自动化稳定性。