postman version:
Postman for Windows
Version 7.29.0
win32 10.0.19041 / x64
Response 响应体样例
{
"statusCode": "AAAXXXBBB0000",
"message": "Success",
"messageZh": null,
"errMsgArgs": null,
"traceId": "123",
"timestamp": null,
"nonceStr": null,
"sign": null,
"data": {
"lastUpdateByName": "System",
"ica": "300001",
"changes": null,
"questions": null,
"parReId": "47023832233864",
"insertTimestamp": "2020-12-21T07:05:41.947+0000",
"deleteFlag": null,
"lastUpdateTimestamp": "2020-12-21T06:49:55.263+0000",
"attachment": null,
"context": null,
"action": "I",
"paramDefCode": "XXBBB005",
"requestType": "XXBBB005_A",
"parallelStepData": null,
"version": 2,
"step": null,
"insertBy": "AutomationTestUser",
"parameters": "{\"cid\": \"88812112006\", \"ica\": \"121106\", \"locked\": false, \"paramId\": 466647024092659712, \"version\": 0, \"insertBy\": \"AutomationTestUser\", \"attachment\": \"{\\\"fileIndexName\\\":\\\"1ed2083f44c433ec769f40f8fa7b1\\\",\\\"fileName\\\":\\\"1573652742438-XXXXXXXX.txt\\\",\\\"downloadFileId\\\":\\\"130816595474798\\\"}\", \"deleteFlag\": false, \"lastUpdateBy\": \"AutomationTestUser\", \"insertTimestamp\": 1608533395213, \"xxx\": \"123456789.12\", \"lastUpdateTimestamp\": 1608533395213}",
"stepCount": null,
"immediateEffective": true,
"insertByName": "AutomationTestUser",
"cid": "135246",
"lastUpdateBy": "System",
"status": "EFFECT"
},
"successFlag": true,
"deEncryptFlag": false
}
对上面的响应进行断言 Tests
1. 取到paramId, paramReqId
pm.globals.unset("paramId");pm.globals.unset("paramReqId");
var jsonData = pm.response.json();
var paramId = JSON.parse(jsonData.data.parameters).paramId; // 这里面有个问题,实际获取的值会把后两位的值变成两个0,而不是实际 响应里的值,尝试了强转也不行
console.log("paramId\n", paramId);
var paramReqId = jsonData.data.paramReqId;
console.log("paramReqId\n", paramReqId);
pm.globals.set("paramId",paramId);
pm.globals.set("paramReqId", paramReqId);
// 特定字段值校验
pm.test("xxx value", function () {
var jsonData = pm.response.json();
console.log("校验xxx 值\n");
var xxxValue= JSON.parse(jsonData.data.parameters).xxx;
console.log("xxx value\n", xxxValue);
pm.expect(parseFloat(xxx)).to.eql(pm.globals.get("xxx"));
});
2. 一般性校验
pm.test("Body matches string", function () {
pm.expect(pm.response.text()).to.include("Success");
});
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});
pm.test("has xxx", function () {
pm.expect(pm.response.text()).to.include("xxx");
});
pm.test("has cid", function () {
pm.expect(pm.response.text()).to.include("8881211200");
});
pm.test("Content-Type is present", function () {
pm.response.to.have.header("Content-Type");
});
pm.test("Response time is less than 200ms", function () {
pm.expect(pm.response.responseTime).to.be.below(400); // 单位 ms
});
pm.test("Successful POST request", function () {
pm.expect(pm.response.code).to.be.oneOf([200, 201,202]);
});
pm.test("Status code name has string", function () {
pm.response.to.have.status("OK"); // or OK/Created
});