postmanTests可設置/清除變量, 對比返回值信息。經常使用方法集合彙總以下:html
1.設置環境變量git
postman.setEnvironmentVariable("key", "value"); pm.environment.get("key", "value");
2.設置全局變量github
postman.setGlobalVariable("key", "value"); pm.globals.set("variable_key", "variable_value");
3.檢查response body中是否包含某個stringjson
tests["Body matches string"] = responseBody.has("string_you_want_to_search"); pm.test("Body is correct", function () { pm.response.to.have.body("response_body_string"); });
4.檢測JSON中的某個值是否等於預期的值數組
var data = JSON.parse(responseBody); tests["Your test name"] = data.value === 100;
JSON.parse()方法,把json字符串轉化爲對象。parse()會進行json格式的檢查是一個安全的函數。 安全
如:檢查json中某個數組元素的個數(這裏檢測programs的長度)less
var data = JSON.parse(responseBody); tests["program's lenght"] = data.programs.length === 5;
5.轉換XML body爲JSON對象函數
var jsonObject = xml2Json(responseBody); tests["Body is correct"] = responseBody === "response_body_string";
6.檢查response body是否與某個string相等post
7.測試response Headers中的某個元素是否存在(如:Content-Type)測試
//getResponseHeader()方法會返回header的值,若是該值存在 tests["Content-Type is present"] = postman.getResponseHeader("Content-Type"); tests["Content-Type is present"] = responseHeaders.hasOwnProperty("Content-Type");
上面的方法,不區分大小寫。下面的方法,要區分大小寫。
8.驗證Status code的值
tests["Status code is 200"] = responseCode.code === 200; pm.test("Status code is 200", function () { pm.response.to.have.status(200); });//5.0以上版本方法
9.驗證Response time是否小於某個值
tests["Response time is less than 200ms"] = responseTime < 200; //5.0以上版本方法 pm.test("Response time is less than 200ms", function () { pm.expect(pm.response.responseTime).to.be.below(200); });
10.name是否包含某個值
tests["Status code name has string"] = responseCode.name.has("Created"); pm.test("Status code name has string", function () { pm.response.to.have.status("Created"); });
11.POST 請求的狀態響應碼是不是某個值
tests["Successful POST request"] = responseCode.code === 201 || responseCode.code === 202; //5.0以上版本方法 pm.test("Successful POST request", function () { pm.expect(pm.response.code).to.be.oneOf([201,202]); });
12.很小的JSON數據驗證器
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);
var Json = JSON.parse(request.data);
data {object}:
this is a dictionary of form data for the request. (request.data["key"]=="value")
headers {object}:
this is a dictionary of headers for the request (request.headers["key"]=="value")
method {string}:
GET/POST/PUT etc.
url {string}:
the url for the request.
var Json = JSON.parse(request.data); var version = Json["version"];
14.JSON.parse()和JSON.stringify()
JSON.parse()【從一個字符串中解析出json對象】 JSON.stringify()【從一個對象中解析出字符串】 var data={name:'goatling'} JSON.parse(data) 結果是: '{"name":"goatling"}'
JSON.stringify(data)
結果是:
name:"goatling"
15.判斷字段值是否爲空typeof()
var Jsondata = JSON.parse(responseBody); if( typeof(Jsondata.data) != "undefined" )
注:文章轉自https://www.cnblogs.com/JHblogs/p/6418802.html
原文中14描述有誤,已在文中修改。