Postman中的Tests工具可以帮助我们写一些非常简单的断言帮助我们测试,如果你不熟悉JavaScript语法的话postman还为用户提供了几种模板来写断言。
以下是几种比较常用的断言
pm.test("响应状态码为200", function () {
pm.response.to.have.status(200);
});
pm.test("响应数据中包含json_key1", function () {
pm.expect(pm.response.text()).to.include("json_key1");
});
pm.test("响应中的url值正确", function () {
var jsonData = pm.response.json();
pm.expect(jsonData.url).to.eql("http://10.0.6.2/post");
});
pm.test("Body is correct", function () {
pm.response.to.have.body("response_body_string");
});
pm.test("响应头信息中包含Content-Type", function () {
pm.response.to.have.header("Content-Type");
});
pm.test("响应时间小于300ms", function () {
pm.expect(pm.response.responseTime).to.be.below(300);
});