Let's see some examples of postman tests. Most of them are as internal postman snippets. Most tests are as simple as a single-line JavaScript statement. You can have as many tests as you want with one request.
Note: The test script runs after a response has been received from the server.
Test example
1. Set environment variables
postman.setEnvironmentVariable("key", "value");
Example: postman.setEnvironmentVariable("url", "http://192.168.36.47/v2/api");
Use the format of environment variables : {{url}}
1.1 Clear the environment variable
postman.clearEnvironmentVariable("variable_key");
Example: postman.clearEnvironmentVariable("url");
2. Set a global variable
postman.setGlobalVariable("key", "value");
Example : postman.setGlobalVariable("username", "[email protected]");
Use the global variable format: {{variableName}}
2.1 Clear a global variable
postman.clearGlobalVariable("key", "value");
3. Check that the response body contains a string
tests["Body matches string"] = responseBody.has("string_you_want_to_search");
Example: The response body contains the following field "path": "field is read-only",
tests["Body matches string"] = responseBody.has("field is read-only");
tests["Body matches string"] = responseBody.has("path");
4. Convert XML body to JSON object
var jsonObject = xml2Json(responseBody) ;
Example:
5.Check that the response body is equal to a string
tests["Body is correct"] = responseBody === "response_body_string";
Example: The response body contains the following field "path": "field is read-only",
tests[ "Body is correct"] = responseBody === "response_body_string";
6. Check a JSON value
var data = JSON.parse(responseBody);
tests["Your test name"] = data.value === 100;
7. Existence of Content-Type (case-insensitive check)
tests["Content-Type is present"] = postman.getResponseHeader("Content-Type"); //Note: the getResponseHeader() method returns the header value, if it exists.
8. Content-Type exists (size-sensitive write)
tests["Content-Type is present"] = responseHeaders.hasOwnProperty("Content-Type");
9.
tests["Response time is less than 200ms"] = responseTime < 200;
10. Status The code is 200
tests["Status code is 200"] = responseCode.code === 200;
Example: The status code is 404
tests["Status code is 404"] = responseCode.code === 404;
11. The code contains a String
tests["Status code name has string"] = responseCode.name.has("Created");
Example: Status: 201 CREATED
tests[" Status code is 201"] = responseCode.code === 201;
tests["Status code name has string"] = responseCode.name.has("Created");
12. Status code of successful POST request
tests["Successful POST request"] = responseCode.code === 201 || responseCode .code === 202;
13. JSON data using TinyValidator
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); the
sample data file
JSON file is given by Key/value pairs
Download JSON file
For CSV files, the top line needs to contain variable names
Download CSV file
Let's see some examples of postman tests. Most of them are as internal postman snippets. Most tests are as simple as a single-line JavaScript statement. You can have as many tests as you want with one request.
Note: The test script runs after a response has been received from the server.
Test example
1. Set environment variables
postman.setEnvironmentVariable("key", "value");
Example: postman.setEnvironmentVariable("url", "http://192.168.36.47/v2/api");
Use the format of environment variables : {{url}}
1.1 Clear the environment variable
postman.clearEnvironmentVariable("variable_key");
Example: postman.clearEnvironmentVariable("url");
2. Set a global variable
postman.setGlobalVariable("key", "value");
Example : postman.setGlobalVariable("username", "[email protected]");
Use the global variable format: {{variableName}}
2.1 Clear a global variable
postman.clearGlobalVariable("key", "value");
3. Check that the response body contains a string
tests["Body matches string"] = responseBody.has("string_you_want_to_search");
Example: The response body contains the following field "path": "field is read-only",
tests["Body matches string"] = responseBody.has("field is read-only");
tests["Body matches string"] = responseBody.has("path");
4. Convert XML body to JSON object
var jsonObject = xml2Json(responseBody) ;
Example:
5.Check that the response body is equal to a string
tests["Body is correct"] = responseBody === "response_body_string";
Example: The response body contains the following field "path": "field is read-only",
tests[ "Body is correct"] = responseBody === "response_body_string";
6. Check a JSON value
var data = JSON.parse(responseBody);
tests["Your test name"] = data.value === 100;
7. Existence of Content-Type (case-insensitive check)
tests["Content-Type is present"] = postman.getResponseHeader("Content-Type"); //Note: the getResponseHeader() method returns the header value, if it exists.
8. Content-Type exists (size-sensitive write)
tests["Content-Type is present"] = responseHeaders.hasOwnProperty("Content-Type");
9.
tests["Response time is less than 200ms"] = responseTime < 200;
10. Status The code is 200
tests["Status code is 200"] = responseCode.code === 200;
Example: The status code is 404
tests["Status code is 404"] = responseCode.code === 404;
11. The code contains a String
tests["Status code name has string"] = responseCode.name.has("Created");
Example: Status: 201 CREATED
tests[" Status code is 201"] = responseCode.code === 201;
tests["Status code name has string"] = responseCode.name.has("Created");
12. Status code of successful POST request
tests["Successful POST request"] = responseCode.code === 201 || responseCode .code === 202;
13. JSON data using TinyValidator
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); the
sample data file
JSON file is given by Key/value pairs
Download JSON file
For CSV files, the top line needs to contain variable names
Download CSV file