在 Debian 上用 Postman 监控发送成功率的可落地方案
一 核心思路
二 在 Postman 中定义成功判据与测试脚本
// 1) 基础成功判定
pm.test("Status is 2xx", () => {
pm.expect(pm.response.code).to.be.oneOf([200,201,202,204]);
});
// 2) 解析 JSON 并校验业务字段
let json;
try { json = pm.response.json(); } catch (e) {
pm.test("Response is valid JSON", () => { throw e; });
}
pm.test("Success flag or messageId present", () => {
pm.expect(
json.status === "success" ||
(json.messageId && json.messageId !== "")
).to.be.true;
});
pm.test("Required fields exist", () => {
pm.expect(json).to.have.property("to").and.not.empty;
pm.expect(json).to.have.property("subject").and.not.empty;
});
// 3) 累计计数(用于计算成功率)
let total = pm.collectionVariables.get("total") || 0;
let passed = pm.collectionVariables.get("passed") || 0;
total += 1;
if (pm.response.code >= 200 && pm.response.code < 300 &&
(json.status === "success" || (json.messageId && json.messageId !== ""))) {
passed += 1;
}
pm.collectionVariables.set("total", total);
pm.collectionVariables.set("passed", passed);
// 4) 可选:记录本次时延(ms)
let duration = pm.response.responseTime;
pm.collectionVariables.set("lastDuration", duration);
三 批量回放与成功率计算
四 在 Debian 上的落地与自动化
npm install -g newmannewman run send-email.postman_collection.json -e prod.postman_environment.json --reporters cli,json,html --reporter-json-export result.json --reporter-html-export report.html