CentOS 上基于 Postman 的自动化测试实践
一 环境准备与安装
sudo yum install -y nodejs npmwget 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/postmansudo touch /usr/share/applications/postman.desktop[Desktop Entry]
Encoding=UTF-8
Name=Postman
GenericName=Api Tools
Comment=Postman
Exec=/usr/bin/postman
Terminal=false
MimeType=text/plain
Icon=/usr/local/postman/Postman/app/resources/app/assets/icon.png
StartupNotify=true
Categories=Development;
sudo chmod +x /usr/share/applications/postman.desktoppostman 启动 GUI;或后续用 newman -v 验证 CLI 可用性。二 手工到自动化的关键配置
// 设置时间戳请求头
pm.environment.set("timestampHeader", new Date().toISOString());
// 动态 Authorization
const token = pm.environment.get("TOKEN");
pm.request.headers.add({ key: "Authorization", value: "Bearer " + token });
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("Content-Type is application/json", () => {
pm.response.to.have.header("Content-Type", /application\/json/);
});
pm.test("Response has userId", () => {
const json = pm.response.json();
pm.expect(json).to.have.property("userId");
});
三 使用 Newman 在 CentOS 上做自动化与报告
sudo npm install -g newmannewman run my-collection.json -e dev.env.json-n 10-d data.csv 或 -d data.json--delay-request 500(毫秒)--silent / --verbosesudo npm install -g newman-reporter-html
newman run my-collection.json -e dev.env.json -r html --reporter-html-export report.html
newman-reporter-junitfull、newman-reporter-cli-summary 等,可按需安装并指定 -r 使用。四 持续集成与监控
五 常见问题与排查要点
ls -l /usr/bin/postman;或检查 PATH 是否包含 Postman 目录。--insecure 参数(仅测试环境建议),或正确配置 CA 证书链。