centos下postman如何使用Runner
导读:CentOS 下 Postman Runner 使用指南 一 概念与两种方式 Runner 是 Postman 的集合运行器,用于按指定顺序批量执行请求,支持环境切换、迭代次数、请求延迟、数据文件(CSV/JSON)参数化、响应保存与变量...
CentOS 下 Postman Runner 使用指南
一 概念与两种方式
- Runner 是 Postman 的集合运行器,用于按指定顺序批量执行请求,支持环境切换、迭代次数、请求延迟、数据文件(CSV/JSON)参数化、响应保存与变量持久化等能力。适合在界面中做本地回归与调试。
- 在 CentOS 服务器无界面场景,使用 Newman(Postman 的命令行运行器)执行导出的集合与环境文件,便于集成 CI/CD 与定时任务。
二 在图形界面使用 Runner
- 启动方式
- 点击顶部工具栏的 Runner 按钮,或进入某个 Collection 后从其页面进入 Runner。
- 关键配置
- 执行顺序:通过拖拽调整;可勾选禁用某些请求,实现选择性执行。
- Environment:选择要使用的环境。
- Iterations:设置迭代次数。
- Delay:设置请求间延迟(毫秒)。
- Data:导入 CSV/JSON 数据文件进行参数化(字段名需与脚本变量一致)。
- Save responses:控制响应日志保存策略(全部/仅失败/不保存)。
- keep variable values:运行结束后保留并更新变量。
- Save Cookies after collection run:将运行期间产生的 Cookie 写回 Cookie 管理器。
- 运行与查看
- 点击 Run 开始执行,结果面板展示每个请求的响应、断言结果与耗时等,便于定位问题。
三 在 CentOS 服务器用 Newman 运行集合
- 安装 Node.js 与 Newman
- 安装:sudo yum install -y nodejs npm
- 验证:node -v、npm -v
- 全局安装 Newman:sudo npm install -g newman
- 验证:newman -v
- 准备运行资产
- 在 Postman 中导出 Collection(JSON) 与 Environment(JSON),上传到 CentOS(如 /opt/postman-tests/)。
- 基本运行
- 命令示例:newman run /opt/postman-tests/collection.json -e /opt/postman-tests/environment.json
- 常用选项
- 迭代与延迟:-n 5 --delay-request 1000
- 生成报告:-r cli,json,html;报告目录:–reporter-html-export report.html
- 静默与详细:–silent、–verbose
- 全局变量文件:-g globals.json
- 数据驱动:-d data.csv 或 -d data.json
- 数据驱动示例
- CSV(UTF-8 无 BOM,首行为字段名)
- 文件内容:
username,password user1,pass1 user2,pass2 - 命令:newman run collection.json -e environment.json -d data.csv
- 文件内容:
- JSON
- 文件内容:
[ { "username":"user1","password":"pass1"} , { "username":"user2","password":"pass2"} ] - 命令:newman run collection.json -e environment.json -d data.json
- 文件内容:
- 在请求或预请求脚本中使用数据(示例):
- pm.collectionVariables.set(“username”, pm.iterationData.get(“username”))
- pm.collectionVariables.set(“password”, pm.iterationData.get(“password”))
- 断言示例:pm.test(“Status code is 200”, () => pm.response.to.have.status(200));
- CSV(UTF-8 无 BOM,首行为字段名)
- 自动化与守护
- Shell 脚本(/opt/postman-tests/run.sh):
#!/bin/bash COLLECTION=/opt/postman-tests/collection.json ENV=/opt/postman-tests/environment.json REPORT=/opt/postman-tests/report.html newman run "$COLLECTION" -e "$ENV" -r cli,html --reporter-html-export "$REPORT"- 赋权并运行:chmod +x run.sh & & ./run.sh
- systemd 服务(/etc/systemd/system/postman-runner.service):
[Unit] Description=Newman Postman Collection Runner After=network.target [Service] Type=simple ExecStart=/usr/local/bin/newman run /opt/postman-tests/collection.json -e /opt/postman-tests/environment.json -r cli,html --reporter-html-export /opt/postman-tests/report.html Restart=on-failure User=your_username [Install] WantedBy=multi-user.target- 启用与启动:sudo systemctl daemon-reload & & sudo systemctl enable --now postman-runner.service & & sudo systemctl status postman-runner.service。
- Shell 脚本(/opt/postman-tests/run.sh):
四 常见问题与排查
- CSV 中文乱码:用编辑器(如 Notepad++)转换为 UTF-8 再导入;Runner 与 Newman 均以 UTF-8 解析数据文件。
- 迭代次数与数据行数:Runner 会根据数据文件行数(不含表头)自动填充迭代次数;Newman 使用 -n 指定迭代,若同时使用 -d,通常建议 -n 与数据行数一致,或省略 -n 让 Newman 按数据行自动迭代。
- 变量与 Cookie 持久化:Runner 界面可勾选 keep variable values 与 Save Cookies after collection run;Newman 默认不会持久化变量与 Cookie,如需在多次运行间共享,请使用 -g globals.json 管理全局变量,或在脚本中显式写入环境/集合变量,Cookie 需由被测服务处理或使用会话保持机制。
声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!
若转载请注明出处: centos下postman如何使用Runner
本文地址: https://pptw.com/jishu/765251.html
