Linux下Postman响应验证实操指南
一 核心思路与入口
二 常用断言示例
pm.test("Status code is 200", () => {
pm.response.to.have.status(200);
});
pm.test("Body contains 'example'", () => {
pm.expect(pm.response.text()).to.include("example");
});
pm.test("JSON has valid fields", () => {
const jsonData = pm.response.json();
pm.expect(jsonData).to.have.property("id").that.is.a("number");
pm.expect(jsonData.name).to.be.a("string").and.not.empty;
});
pm.test("Content-Type is JSON", () => {
const ct = pm.response.headers.get("Content-Type");
pm.expect(ct).to.include("application/json");
});
pm.test("Response time < 200ms", () => {
pm.expect(pm.response.responseTime).to.be.below(200);
});
pm.test("XML field value", () => {
const json = xml2Json(pm.response.text());
pm.expect(json.root.key).to.eql("value");
});
console.log("Response JSON:", pm.response.json());
以上示例可直接粘贴到请求的 Tests 面板使用。
三 批量运行与命令行自动化
# 安装 Newman
sudo npm install -g newman
# 运行集合与环境
newman run collection.json -e environment.json
# 生成 HTML 报告
newman run collection.json -e environment.json \
--reporters html --reporter-html-export report.html
四 调试技巧与常见问题