postman内置了8中断言,基本满足了自动化大部分场景需要:
接口返回的状态码=指定值
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});
响应内容存在指定文本
pm.test("Body matches string", function () {
pm.expect(pm.response.text()).to.include("string_you_want_to_search");
});
响应的是json,且某个字段=指定值
pm.test("Your test name", function () {
var jsonData = pm.response.json();
pm.expect(jsonData.value).to.eql(100);
});
响应内容完全匹配=某个内容
pm.test("Body is correct", function () {
pm.response.to.have.body("response_body_string");
});
响应头包含指定key
pm.test("Content-Type is present", function () {
pm.response.to.have.header("Content-Type");
});
响应时间在指定范围内
pm.test("Response time is less than 200ms", function () {
pm.expect(pm.response.responseTime).to.be.below(200);
});
状态码原因短语=指定值
pm.test("Status code name has string", function () {
pm.response.to.have.status("OK");
});
响应码在指定范围
pm.test("Successful POST request", function () {
pm.expect(pm.response.code).to.be.oneOf([201, 202]);
});
但其实大家不乐意记这么多。。。。
很简单,postman直接引用,只需要熟悉下使用说明文案:
test编辑窗口下,postman界面右侧,snippet工具 (截图的
(上面示例与截图顺序一致)