First test case

Postman can write and run tests for each request. Postman tests are essentially JavaScript code that sets values for a particular test object

1. Select GET as the request mode and enter www.baidu.com as shown below

 

2. Write a test script in the Tests text field to determine the content of the response, for example:

tests[“Status code is 200”] = responseCode.code === 200;

Tests [“Body matches String “] = responseBody.has();

 

 

3. Click “Send” to see the test result:

4. Failure of use cases:

 

 

Second, you don’t need to remember so much syntax when writing test scripts. You can simplify this process by listing common code snippets alongside the editor. You can select the snippet to add and add the corresponding code to the test editor.

Built-in script description:

 

1. The removal of a global variable Clear a global variable corresponding script: postman. ClearGlobalVariable (" variable_key "); Parameters: the need to remove the key of the variables of (2) removal of an environment variable Clear an environment variable corresponding script: postman. ClearEnvironmentVariable (" variable_key "); Parameter: key of the environment variable to be cleared 3. Response Contains content Response Body :Contains string  tests["Body matches string"] =responseBody.has("string_you_want_to_search"); Response body:Convert XML body to a JSON Object  var jsonObject = xml2Json(responseBody); XML to be converted 5. Response Is equal to the expected content response Body :Is equal to a string  tests["Body is correct"] = responseBody === "response_body_string"; Parameter: expected response 6. Json Parses the value of the key for verification Response Body: json value check Tests ["Args key contains argument passed as URL parameter"] = 'test' in responseJSON. Check whether the header information of response headers: Content-type header check  tests["Content-Type is present"] = postman.getResponseHeader("Content-Type"); Header 8. Check the Response time is less than 200ms.  tests["Response time is less than 200ms"] = responseTime < 200; Parameters: the response time is 9. Set the global variable Set an global variable corresponding script: postman. SetGlobalVariable (" variable_key ", "variable_value"); Parameter: key of global variable 10. Set an environment variable  postman.setEnvironmentVariable("variable_key", "variable_value"); If tests["Status code is 200"] = responsecode.code! If tests["Status code is 200"] = responsecode.code! = 400; Parameter: Status code 12. Check whether code Name contains content Status code:Code Name has string  tests["Status code name has string"] = responseCode.name.has("Created"); 13. Successful POST request Status code:Successful Post Request  tests["Successful POST request"] = responseCode.code === 201 || responseCode.code === 202; 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); Parameters: You can modify key-value pairs in items to validate JSON parametersCopy the code