常用的postman斷言

常用的postman斷言 解釋 對應腳本
Response body:Contains string response包含字符串 tests["Body matches string"] = responseBody.has("string_you_want_to_search");
Response body:Is equal to a string response body等於指定字符串 tests["Body is correct"] = responseBody === "response_body_string";
response body:JSON value check json解析key的值進行校驗 tests["Your test name"] = jsonData.value === 100;
status code:Code is 200 判斷狀態碼 tests["Status code is 200"] = responseCode.code === 200;
status code:code name has string 檢查code name 是否包含內容 tests["Status code name has string"] = responseCode.name.has("Created");

postman斷言解析

 

postman斷言是JavaScript語言編寫的,在postman客戶端指定區域編寫即可。

斷言會在請求返回之後,運行,並根據斷言的pass\fail情況體現在最終測試結果中。

 

1.設置環境變量--Setting an environment variable 

postman.setEnvironmentVariable("key", "value");

2.設置全局變量--Set a global variable

postman.setGlobalVariable("key", "value");

3.檢查響應中包含string--Check if response body contains a string

tests["Body matches string"] = responseBody.has("string_you_want_to_search");

4.轉化XML格式的響應成JSON對象---Convert XML body to a JSON object

var jsonObject = xml2Json(responseBody);

5.檢查響應body中等於指定string--Check if response body is equal to a string

 tests["Body is correct"] = responseBody === "response_body_string";

6.檢查JSON某字段值--Check for a JSON value

var data = JSON.parse(responseBody);

tests["Your test name"] = data.value === 100;

7.檢查Content-Type是否包含在header返回(大小寫不敏感)--Content-Type is present (Case-insensitive checking)

 tests["Content-Type is present"] = postman.getResponseHeader("Content-Type"); //Note: the getResponseHeader() method returns the header value, if it exists.

8.檢查Content-Type是否包含在header返回(大小寫敏感)--Content-Type is present (Case-sensitive)

 tests["Content-Type is present"] = responseHeaders.hasOwnProperty("Content-Type");

9.檢查請求耗時時間小於200ms--Response time is less than 200ms

tests["Response time is less than 200ms"] = responseTime < 200;

10.檢查Status code爲200--Status code is 200

tests["Status code is 200"] = responseCode.code === 200;

11.檢查Code name包含指定string--Code name contains a string

 tests["Status code name has string"] = responseCode.name.has("Created");

12.檢查成功post的請求status code--Succesful POST request status code

tests["Successful POST request"] = responseCode.code === 201 || responseCode.code === 202;

13.爲JSON data使用微小驗證器--Use TinyValidator for JSON data

var schema = {

 "items": {

 "type": "boolean"

 }

};

var data1 = [true, false];

var data2 = [true, 123]; 

console.log(tv4.error);

tests["Valid Data1"] = tv4.validate(data1, schema);

tests["Valid Data2"] = tv4.validate(data2, schema);

Sample data files 

JSON files are composed of key/value pairs

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章