在test里有一些代码片段可以直接调用:

postman用法-断言练习 随笔 第1张

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。

代码1:

postman用法-断言练习 随笔 第2张

pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});判断该请求的状态码是否200,一般200就是接口调通了的意思

"Status code is 200"这个是可以自定义的方法名称

pm即postman

代码2:

postman用法-断言练习 随笔 第3张

pm.test("Body matches string", function () {
pm.expect(pm.response.text()).to.include("string_you_want_to_search");
});判断接口的返回值知否包含某参数等

例如:

postman用法-断言练习 随笔 第4张

如果接口返回值中包含insurant这个字段,在TestResults里就会显示pass

代码3:

postman用法-断言练习 随笔 第5张

pm.test("Your test name", function () {
var jsonData = pm.response.json();  
pm.expect(jsonData.value).to.eql(100);
}); 先将接口返回内容转为json格式,再校验键值对

例如,我想校验这个接口返回的内容里的某个字段是否为某个值:postman用法-断言练习 随笔 第6张

postman用法-断言练习 随笔 第7张

原代码中的jsonData.value即为键名,在我这就是字段username,原代码中的100即为键值,在我这就是username的值,键名的格式为jsonData.xxx.xxx

代码4:

postman用法-断言练习 随笔 第8张

pm.test("Body is correct", function () {
pm.response.to.have.body("response_body_string");
}); 检验接口返回内容是否和预期一致

postman用法-断言练习 随笔 第9张

 

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄