1 断言
postman借助JavaScript - js语言编写代码,自动判断预期结果与实际结果是否一致
断言代码 写在 Tests 的标签中
1-1 断言响应状态码
Status code: Code is 200
pm: 代表postman的一个实例
test(): 是 pm实例的一个方法,有两个参数
参数1:在断言成功后,给出的文字提示,可以修改
参数2:匿名函数
{}:函数体
pm.response.to.have.status(200); postman的响应结果中应该包含状态码 200
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});
1-2 断言响应体是否包含某个字符串
Response body: Contains string
pm: 代表postman的一个实例
test(): 是 pm实例的一个方法,有两个参数
参数1:在断言成功后,给出的文字提示,可以修改
参数2:匿名函数
{}:函数体
pm.response.to.have.status(200); postman的响应结果中应该包含所要搜索的字符串
pm.test("Body matches string", function () {
pm.expect(pm.response.text()).to.include("string_you_want_to_search");
});
1-3 断言响应体是否等于某个字符串(对象)
Response body: is equal to a string
断言响应体是否等于某个字符串(对象)
pm.test("Body is correct", function () {
pm.response.to.have.body("response_body_string");
});
1-4 断言json数据
Response body: JSON value check
断言 json结果中的某个key的值是100
pm.test("Your test name", function () {
var jsonData = pm.response.json();
pm.expect(jsonData.value).to.eql(100);
});
1-5 断言响应头
Response headers: Content-Type header check
pm.test("Content-Type is present", function () {
pm.response.to.have.header("Content-Type");
});