前言
Postman目前是一款很火的接口測試工具,它有着很是強大結果判斷能力。
爲何說強大呢,由於Postman有自帶的校驗腳本,根本不須要咱們去學習JS腳本語言,對於代碼能力爲0的各位測試小夥伴來講,特別的友好。
json
經過Tests的代碼校驗,能夠很快的獲得結果判斷。若是校驗經過,則斷言爲PASS,若是校驗失敗,則斷言爲FAILless
Response body:Contains string (校驗返回結果中是否包含某個字符串)
代碼以下:
pm.test("Body matches string", function () {
pm.expect(pm.response.text()).to.include("string_you_want_to_search");
});
工具
例子:post
![](http://static.javashuo.com/static/loading.gif)
結果:學習
![](http://static.javashuo.com/static/loading.gif)
Response body:Is equal to a string (校驗返回結果是否等於該字符串)
注意: 這個校驗,必須是接口的返回結果與字符串要如出一轍。
代碼以下:
pm.test("Body is correct", function () {
pm.response.to.have.body("response_body_string");
});
測試
例子:ui
![](http://static.javashuo.com/static/loading.gif)
結果:spa
![](http://static.javashuo.com/static/loading.gif)
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);
});
例子: 3d
結果:code
![](http://static.javashuo.com/static/loading.gif)
Response header:Content-type header check(校驗響應頭是否包含某個值)
代碼以下:
pm.test("Content-Type is present", function () {
pm.response.to.have.header("Content-Type");
});
例子:
結果:
![](http://static.javashuo.com/static/loading.gif)
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:
![](http://static.javashuo.com/static/loading.gif)
Status code:Code is 200(校驗響應頭是否包含某個值)
代碼以下:
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});
例子PASS:
例子FAIL:
![](http://static.javashuo.com/static/loading.gif)