Postman断言(Tests)语句基础使用教程[憧憬]
(绿色星星):常用语句 (星星):功能重复 灰色:不常用
区别在于:Pre-request Script是在request之前就会执行代码,而Tests是在request和response结束后的断言区域
(绿色星星) 0、把responseBody值转为JSON格式赋给yue
var yue = JSON.parse(responseBody)
(绿色星星) 1、设置环境变量–Setting an environment variable
postman.setEnvironmentVariable("key", "value");
(绿色星星) 2、设置全局变量–Set a global variable
postman.setGlobalVariable("key", "value");
(绿色星星) 3、检查响应中是否包含字符串XXX–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;
数组取法:tests["1111"]= yueqiang.data.schoolList[0].id===91;
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、获取数组里最后一条记录
var yue=JSON.parse(responseBody);
postman.setEnvironmentVariable("smart-classid",yue.data.formal[yue.data.formal.length-1].id);