postman断言内容详解
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了postman断言内容详解相关的知识,希望对你有一定的参考价值。
参考技术A 1.设置环境变量--Setting an environment variablepostman.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;
Postman断言
postman常见断言方法介绍:
1、Clear a global variable (清除一个全局变量)
postman.clearGlobalVariable("variable_key");
2、Clear an environment variable (清除一个环境变量)
postman.clearEnvironmentVariable("variable_key");
3、Response body:Contains string (返回消息体中包含某个内容)
tests["Body matches string"] = responseBody.has("string_you_want_to_search");
4、Response body:Convert XML body to a JSON Object (将xml格式转换成json格式)
var jsonObject = xml2Json(responseBody);
5、Response body:Is equal to a string (返回的消息体等于某个字符串)
tests["Body is correct"] = responseBody === "response_body_string";
6、Response body:JSON value check (json值校验)
var jsonData = JSON.parse(responseBody); tests["Your test name"] = jsonData.value === 100;
7、Response headers:Content-Type header check (检查消息头中是否有某个字段)
tests["Content-Type is present"] = postman.getResponseHeader("Content-Type");
8、Response time is less than 200ms (响应时间判断)
tests["Response time is less than 200ms"] = responseTime < 200;
9、Set a global variable (设置全局变量)
postman.setGlobalVariable("variable_key", "variable_value");
10、Setting an environment variable (设置一个环境变量)
postman.setEnvironmentVariable("variable_key", "variable_value");
11、Status code:Code is 200 (判断状态码)
tests["Status code is 200"] = responseCode.code === 200;
12、Status code:Code name has string (检查code name 是否包含内容)
tests["Status code name has string"] = responseCode.name.has("Created");
13、Status code:Successful POST request (成功的post请求)
tests["Successful POST request"] = responseCode.code === 201 || responseCode.code === 202;
14、Use Tiny Validator for JSON data (验证器)
var schema = { "items": { "type": "boolean" } }; var data1 = [true, false]; var data2 = [true, 123]; tests["Valid Data1"] = tv4.validate(data1, schema); tests["Valid Data2"] = tv4.validate(data2, schema); console.log("Validation failed: ", tv4.error);
以上是关于postman断言内容详解的主要内容,如果未能解决你的问题,请参考以下文章