在 Ubuntu 上使用 Postman 进行跨平台测试
一 安装与准备
- 使用 Snap 安装(推荐):
- 更新并安装 Snap:sudo apt update && sudo apt install snapd
- 安装 Postman:sudo snap install postman --classic
- 手动安装(通用):
- 下载 Linux 包:wget https://dl.pstmn.io/download/latest/linux-x64 -O postman.tar.gz
- 解压到系统目录:sudo tar -xzf postman.tar.gz -C /opt/
- 创建软链便于启动:sudo ln -s /opt/Postman/Postman /usr/local/bin/postman
- 创建桌面启动器(可选):
- 新建文件:sudo nano /usr/share/applications/postman.desktop
- 写入内容:
[Desktop Entry]
Encoding=UTF-8
Name=Postman
Exec=/opt/Postman/Postman
Icon=/opt/Postman/app/resources/app/assets/icon.png
Terminal=false
Type=Application
Categories=Development;
- 赋权与刷新:sudo chmod +x /usr/share/applications/postman.desktop && sudo update-desktop-database
- 启动方式:应用菜单搜索 Postman 或命令行输入 postman。
二 构建跨平台可复用测试
- 创建集合 Collection:将同一业务域的 GET/POST/PUT/DELETE 请求组织在一起,便于批量运行与共享。
- 环境变量与全局变量:为 开发/测试/预发/生产 分别建立环境,使用 {{base_url}}、{{token}} 等变量,在不同平台与环境中复用同一套请求。
- 编写测试脚本 Tests:在响应返回后使用 JavaScript 断言状态码、响应体字段、响应时间等,例如:
pm.test(“Status code is 200”, () => pm.response.to.have.status(200));
pm.test(“Response has id”, () => {
const json = pm.response.json();
pm.expect(json).to.have.property(‘id’);
});
- 数据驱动与前置处理:在 Pre-request Script 中生成时间戳、签名或读取数据文件;在 Tests 中对响应进行结构化解析与校验,保证同一集合可在不同系统与环境下稳定执行。
三 本地自动化运行与多环境验证
- 安装 Newman(Postman 命令行工具):npm install -g newman
- 运行方式:
- 基本运行:newman run mycollection.json
- 指定环境:newman run mycollection.json --environment test.env.json
- 生成报告:newman run mycollection.json --reporters cli,junit --reporter-junit-export report.xml
- 使用 Collection Runner:在 GUI 中点击集合的 Run 按钮,批量运行请求、配置迭代次数与延迟,并查看 Test Results,适合在 Ubuntu 桌面完成快速回归与调试。
四 在 CI/CD 中实现跨平台持续测试
- 在 Ubuntu Runner 中执行(GitHub Actions 示例):
name: Run Postman Tests
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
jobs:
postman-tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: ‘18’
- name: Install Newman
run: npm install -g newman
- name: Run Collection
run: |
newman run “collections/my-api-tests.json”
–environment “environments/test.env.json”
–reporters cli,junit
–reporter-junit-export report.xml
- name: Upload Report
uses: actions/upload-artifact@v4
with:
name: postman-report
path: report.xml
- 要点:将 集合与环境 纳入版本控制;在 CI 中使用 JUnit 报告 与 GitHub Actions 工件 展示结果;同一流水线可在 Ubuntu、Windows、macOS Runner 上执行,从而实现跨平台一致性验证。