Recently in the study Postman Official document, the homeopathic translation comes out, for the study!
The postman assertion is written in the JavaScript language and is written in the Postman client-specified area.
The assertion runs after the request is returned, and is reflected in the final test results based on the pass\fail of the assertion.
1. Setting environment variables--setting an environment variable
Postman.setenvironmentvariable ("Key", "value");
2. Setting global variables--set a global variable
Postman.setglobalvariable ("Key", "value");
3. Check that the response contains String--check If response body contains a string
tests["Body matches string"] = Responsebody.has ("String_you_want_to_search");
4. Convert XML format responses to JSON objects---convert XML body to a JSON object
var jsonobject = Xml2json (responsebody);
5. Check the response body equals the specified String--check If response body is equal to a string
tests["Body is correct"] = Responsebody = = = = "Response_body_string";
6. Check JSON for a field value--check for a JSON value
var data = Json.parse (responsebody);
tests["Your Test name"] = Data.value = = = 100;
7. Check if Content-type is included in header return (case insensitive)--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. Check if Content-type is included in header return (case sensitive)--content-type is present (case-sensitive)
Tests["Content-type is present"] = Responseheaders.hasownproperty ("Content-type");
9. Check the request takes less than 200ms--response time is lesser than 200ms
tests["Response time is less than 200ms"] = ResponseTime < 200;
10. Check Status Code 200--status code is 200
tests["Status code is"] = Responsecode.code = = = 200;
11. Check that the Code name contains the specified String--code name contains a string
tests["Status code name has string"] = ResponseCode.name.has ("Created");
12. Check for successful POST request status Code--succesful Post requests status code
tests["Successful POST request"] = Responsecode.code = = = 201 | | Responsecode.code = = 202;
13. Use tiny validators for 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
Postman Assertion Parsing