前言
Postman目前是一款很火的接口測試工具,它有着非常強大結果判斷能力。
為什么說強大呢,因為Postman有自帶的校驗腳本,根本不需要我們去學習JS腳本語言,對於代碼能力為0的各位測試小伙伴來說,特別的友好。
通過Tests的代碼校驗,可以很快的得到結果判斷。如果校驗通過,則斷言為PASS,如果校驗失敗,則斷言為FAIL
Response body:Contains string (校驗返回結果中是否包含某個字符串)
代碼如下:
pm.test("Body matches string", function () {
pm.expect(pm.response.text()).to.include("string_you_want_to_search");
});
例子:

結果:

Response body:Is equal to a string (校驗返回結果是否等於該字符串)
注意: 這個校驗,必須是接口的返回結果與字符串要一模一樣。
代碼如下:
pm.test("Body is correct", function () {
pm.response.to.have.body("response_body_string");
});
例子:

結果:

Response body:JSON value check(校驗返回結果中某個字段值是否等於某個值)
代碼如下:
pm.test("Your test name", function () {
//設置jsonData變量用來接收postman的json格式的返回數據
var jsonData = pm.response.json();
//判斷返回數據中,msg字段是結果是否為OK
//此處與需要注意一下json格式,jsonData為整個接口的返回數據,jsonData.msg是第一層級字段
pm.expect(jsonData.value).to.eql(100);
});
例子:
結果:

Response header:Content-type header check(校驗響應頭是否包含某個值)
代碼如下:
pm.test("Content-Type is present", function () {
pm.response.to.have.header("Content-Type");
});
例子:
結果:

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);
});
例子PASS:
例子FAIL:

Status code:Code is 200(校驗響應頭是否包含某個值)
代碼如下:
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});
例子PASS:
例子FAIL:
