postman 断言
1.什么是断言
判断程序执行结果是否符合我们的预期就是断言
2.接口测试根据什么判定断言
一般根据程序返回状态码和响应返回数据
3.postman 在什么地方设置断言
在pre-request script 和tests 内设置
pre-request script(预置脚本):在请求发送之前执行
tests script:在接到响应之后执行脚本

4.案例讲解如何设置断言过程
4.1 Url地址:postman-echo.com/post
4.2 断言规则:
. 相应状态码:200
. 相应内容:返回的user参数值和定义的一致
.相应时间:小于0.5s
4.3脚本设置
1.地址栏输入地址

2.pre-request script内定义变量

3.body下定义本地变量

4.test下设置断言的处理
. 相应状态码:200
点击右侧code is 200 在编辑区域会自动生成相应代码

. 相应内容:返回的user参数值和定义的一致
获取参数值
点击get an environment variable ,再修改里面的变量值为user

检查响应内容和请求的是否一致
点击response body:json value check,修改相应参数值

.相应时间:小于0.5s
点击response time is less tahn 200ms,修改相应参数值为500

5.运行脚本
在test results下查看运行结果

6.脚本代码:
pre-request script :
pm.variables.set("user",'zxw');
test script:
//判断响应状态码
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});
//获取参数值
username=pm.variables.get("user");
//检查响应内容和请求的是否一致
console.log(username)
pm.test("check username", function () {
var jsonData = pm.response.json();
pm.expect(jsonData.json['user']).to.eql(username);
});
//响应时间是否小于0.5s
pm.test("Response time is less than 500ms", function () {
pm.expect(pm.response.responseTime).to.be.below(500);
});