在 CentOS 上实现 Postman 自动化测试
一 环境准备与安装
sudo yum install -y nodejs npm(若版本较旧,可启用 NodeSource 仓库升级)。wget https://dl.pstmn.io/download/latest/linux64 -O postman.tar.gzsudo tar -xzf postman.tar.gz -C /optsudo ln -s /opt/Postman/Postman /usr/bin/postman/usr/share/applications/postman.desktop,Exec=/usr/bin/postman,Icon=/opt/Postman/app/resources/app/assets/icon.png。npm install -g newman。二 编写与导出测试集合
pm.environment.set("timestampHeader", new Date().toISOString());。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));pm.test("Has userId", () => pm.expect(pm.response.json()).to.have.property("userId"));{{baseUrl}}/users 引用。三 在 CentOS 上运行自动化测试
newman run collection.json -e environment.jsonconst newman = require('newman');
newman.run({
collection: 'path/to/collection.json',
environment: 'path/to/environment.json'
}, function (err, summary) {
if (err) { console.error(err); process.exit(1); }
console.log(summary);
process.exit(summary.run.failures.length > 0 ? 1 : 0);
});
node run-tests.js-d data.csv 或 -d data.json-r cli,html,json;报告目录通过 --reporter-html-export report.html 指定-g globals.json、-n 10newman run collection.json -e environment.json -d data.csv -r cli,html --reporter-html-export report.html -n 5。四 集成 CI/CD 与报告通知
五 常见问题与实用建议
{{var}} 在请求中引用。-n 指定循环次数,覆盖多组用例。pm.sendRequest 发起前置请求并提取 token 等数据,存入环境变量供后续请求使用。