断言有常规断言,动态参数断言以及全局断言
一、常用的几种断言方式
我们要知道,再进行断言前我们一定要先进行参数化
Status code:code is 200
# 检查返回的状态是否为200
pm.test("Status code is 200", function () {
#标红是可以修改的,如修改为'"状态返回成功"之类
pm.response.to.have.status(200);
});
成功如下图,在响应页面中Test Results中查看,如图
Response body:contains string
#检查响应中包括某个字符串
pm.test("Body matches string", function () {
"Body matches string"这可以修改"检查响应中是否有token",标红为可以修改的
pm.expect(pm.response.text()).to.include("string_you_want_to_search");
这里要注意比如我们要查看"token",所以我们将"string_you_want_to_search",修改为"token"
});
Response body:Json value check
检查响应当中是否有json的值
pm.test("Your test name", function () {
var jsonData = pm.response.json();
pm.expect(jsonData.value).to.eql(100);
这里我们要注意jsonData.不要修改,我们只修改value,和数据100,如我们要检查响应中是否包含status和他的数值,pm.expect(jsonData.staus).to.eql(200);如下图
});
Response body:is equal to a string
检查响应等于某个文本格式下的字符,这适应于字符比较少的
pm.test("Body is correct", function () {
pm.response.to.have.body("response_body_string");
这里我们要主要两点,要在响应页面中Raw文本格式下复制,全部都是复制,2.当大括号有双引号时{""},我们一定要在外面加''单引号,不然就会识别不了
});
Response header:content-type header cheack
检查是否包含响应头content-type
pm.test("Content-Type is present", function () {
pm.response.to.have.header("Content-Type");
});
这里不用修改其他,Content-Type响应头在响应页面中Headers响应头中查看
Response time is less than 200ms
检查响应请求耗时小于200ms
pm.test("Response time is less than 200ms", function () {
pm.expect(pm.response.responseTime).to.be.below(200);
});
Time为时间
二、断言中获取自定义动态参数
1.在请求页面Pre-request Script下开始编写自定义参数脚本
var times =Date.now();
创建一个时间戳(时间戳在上篇有讲到)赋值给times
pm.globals.set("times",times);
设置全局变量,全局变量名为times,值为times
2.在常规断言中设置自定义动态参数,下图中name:其实是码上教育{ {times}},通过动态参数设置为的
接着我们将常规断言进行改变,有三种方法
1.pm.test("响应中是否有name", function () {
pm.expect(pm.response.text()).to.include("码上教育"+pm.globals.set("{ {times}}"));
});
2.pm.expect(pm.response.text()).to.include("码上教育"+globals.set["times"'])
3.pm.expect(pm.response.text()).to.include("码上教育"+globals.times)
三、全局断言
第四步将复制的如状态码200,粘贴在其中