一、postman总共八大断言,其中1/2/3/4/6较为常用
//1、断言返回码为200(用于状态断言)
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});
//2、断言返回的结果中包含一种指定的字符串(用于业务断言)
pm.test("Body matches string", function () {
pm.expect(pm.response.text()).to.include("string_you_want_to_search");
});
//3、对返回的结果做JOSN字段检查(用于业务断言)
pm.test("Your test name", function () {
var jsonData = pm.response.json();
pm.expect(jsonData.value).to.eql(100); //把value换成需要验证的键值对,比如:pm.expect(jsonData.tags[0].id).to.eql(100)
});
//4、断言返回结果等于一个字符串(用于业务断言)
pm.test("Body is correct", function () {
pm.response.to.have.body("response_body_string");
});
//5、断言响应头中包含有指定的响应头
pm.test("Content-Type is present", function () {
pm.response.to.have.header("Content-Type");
});
//6、断言接口请求的时间少于200ms(用于性能断言)
pm.test("Response time is less than 200ms", function () {
pm.expect(pm.response.responseTime).to.be.below(200);
});
//7、断言一个POST请求的返回状态码是否在指定的范围里面
pm.test("Successful POST request", function () {
pm.expect(pm.response.code).to.be.oneOf([201,202]);
});
//8、断言返回的状态码信息中包含指定的字符串
pm.test("Status code name has string", function () {
pm.response.to.have.status("Created");
});
======================================================================================================
在Postman中的位置如图
二、要获取动态参数下的断言结果,需要加前置脚本可捕获返回字段的精确值(比如返回值中有随机字符串)
1、先去Pre-request Script中写下脚本,将其设置为全局变量
var times = Date.now()
pm.globals.set("times", times);
2、再去body中引用,如下:
{
"tag":{
"name":"aaa{{times}}"
}
}
3、再去断言中去get这个全局变量方可使用
======================================================================================================
注1:一般状态断言放在如图位置
注2:写好脚本后用newman非GUI的方式执行
newman run "e:\\yongli.json" -e "e:\\huanjing.json" -g "e:\\quanju.json" -r cli,html,json,junit --reporter-html-export "e:\\report.html"
(1) -e 指定环境变量,把在postman中设置的环境变量导出,然后再把路径填写到这里即可
(2) -g 指定全局变量,把在postman中设置的全局变量导出,然后再把路径填写到这里即可
(3) -n 指定迭代次数,即运行n次脚本
(4) --timeout-request 指定请求的超时时间
(5) -r 指定运行报告的格式,可以为json格式、html格式,默认为cli格式,即在命令行展示运行结果