Postman Assertion Parsing

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.