Postman常用断言
1.断言响应状态码
Status code: Code is 200 步骤: 1.在Tests标签中,选中Status Code:code is 200,生成对应代码 2.适当调整test()方法参数1,和匿名函数中的预期结果; 3.点击send按钮,发送请求,执行断言代码 4.查看断言结果。
pm.test("Status code is 200",function() {
pm.response.to.have.status(200);
});
pm:代表postman的一个实例 test(): 是pm实例的一个方法。有两个参数 参数1:在断言成功后,给出的文字提示。可以修改.“Status code is 200” 参数2:匿名函数 pm.response.to.have.status(200); //意思:postman的响应结果中应该包含状态码200
2.断言响应体是否包含某个字符串
Response body:Contains string
//断言响应体包含指定字符串
pm.test("Body matches string",function () {
pm.expect(pm.response.text()).to.include("string_you_want_to_search");
});
pm:代表postman的一个实例 test(): 是pm实例的一个方法。有两个参数 参数1:断言后显示的文字提示信息,可改 参数2:匿名函数 pm.expect(pm.response.text()).to.include(“string_you_want_to_search”); // 意思:pm 期望 响应文本中,包含****字符串 “string_you_want_to_search” --> 预期结果,可以修改
3.断言响应体是否等于某个字符串(对象)
Response body:Is equal to a string
//断言 响应体 等于某个字符串(对象)
pm.test("Body is correct",function () {
pm.response.to.have.body("response_body_string");
});
pm.response.to.have.body(“response_body_string”); // 意思是,pm的响应中应该有响应体XXX “response_body_string” --> 预期结果,可以修改
4.断言JSON数据
Response body:JSON value check
//断言json的响应结果
pm.test("Your test name",function () {
var jsonData = pm.response.json();
pm.expect(JsonData.value).to.eql(100);
} )
var jsonData = pm.response.json(); pm.expect(JsonData.value).to.eql(100); // var jsonData 用js语法定义一个变量。jsonData就是变量名 // pm.response.json();代表响应的json结果
// 举例:response.json();
/*
{
"success":true,
"code":10000,
"message":"操作成功!",
"data":"95c78d75-721c-40fb-bsd5-742fea42cbd5"
}
*/
pm.expect(JsonData.value).to.eql(100); //pm 预期 json的结果值等于100 to.eql(100) 中的100代表预期结果,是可以修改的。 /* 举例: jsonData.value 的value: 取:success、code、message、data */ 5.断言响应头 Response headers:Content-Type header check
// 断言响应头
pm.test("Content-Type is present",function () {
pm.response.to.have.header("Content-Type");
});
pm.response.to.have.header(“Content-Type”); // pm的响应头 中包含Content-Type
// 示例:可以在header中,添加响应头中的key对应的value判定,用,隔分。
// 断言响应头
pm.test("Content-Type is present",function () {
pm.response.to.have.header("Content-Type","application/json;charset=UTF-8");
});
Postman 断言的工作原理
相关文章
发表评论