postman的常见断言
postman的常见断言
进行接口测试时,添加断言时必不可少的,断言就是判断响应内容与预期返回是否一致。
使用postman来进行断言,需要切换到postman tests 这个模块,在控制台右侧可以看到备用的代码片段。
操作步骤:1、填写断言 2、点击send新的改变
查看断言结果:
postman常见断言方法
1.判断响应内容是否包含某个字符串【Response body:Contains string】
pm.test(“Body matches string”, function () {
pm.expect(pm.response.text()).to.include(“stars”);
});
2.判断响应内容是否和预期内容完全一致【Response body:Is equal to a string】
pm.test(“Body is correct”, function () {
pm.response.to.have.body(“response_body_string”);
});
3.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);
});
4.Status code is 200 (状态码是200)
pm.test(“Status code is 200”, function () {
pm.response.to.have.status(200);
});
5.成功的POST请求状态码【Successful POST request status code】
pm.test(“Successful POST request”, function () {
pm.expect(pm.response.code).to.be.oneOf([201,202]);
});
6.判断返回json数据中某个键对应的值【Response body:JSON value check】
pm.test(“Your test name”, function () {
var jsonData = pm.response.json();
pm.expect(jsonData.value).to.eql(100);
});
其中,Your test name表示这个测试片段的名称,可以自己任意定义;
jsonData.value表示响应json串中的某个键,
7.判断响应体中某个返回值的类型
同样点击选择【Response body:JSON value check】,然后要把jsonData.value放在一个typeof()函数中,这样就是判断输出值的类型了
断言设置好后,点击【Send】发送请求,在响应模块的Test Results标签中可以看到断言结果