postman tests test case

1, returns the status code 200 is
pm.test ( "return status code 200 is", function () {
pm.response.to.have.status (200 is);
});

tests [ "return status code 200"] = responseCode.code === 200;

 

2, the return time is less than 200 milliseconds
pm.test ( "return time is less than 200 ms", function () {
pm.expect (pm.response.responseTime) .to.be.below (200);
});

tests [ "return time is less than 200 ms"] = responseTime <200;
pm.test ( "return the string", function () {

pm.response.to.be.string;
});

3.设置环境变量
postman.setEnvironmentVariable("key", "value");
pm.environment.set("variable_key", "variable_value");

4. exclusive nested set as an environment variable

was = array [1, 2, 3, 4];

postman.setEnvironmentVariable("array", JSON.stringify(array, null, 2));

var obj = { a: [1, 2, 3, 4], b: { c: 'val' } };

postman.setEnvironmentVariable("obj", JSON.stringify(obj));

The environment variables
postman.getEnvironmentVariable ( "key");
Gets an environment variable (a String object whose value is)
var = the JSON.parse Array (postman.getEnvironmentVariable ( "Array"));

var obj = JSON.parse(postman.getEnvironmentVariable("obj"));

6. Remove an environment variable

postman.clearEnvironmentVariable("key");
pm.environment.unset("variable_key");


7. Set a global variable

postman.setGlobalVariable("key", "value");

pm.globals.set("variable_key", "variable_value");

8. Get the global variable


postman.getGlobalVariable("key");

pm.globals.get("variable_key");

9. Clear the global variable

postman.clearGlobalVariable("key");
pm.globals.unset("variable_key");

10. Check whether the response contains a string

tests["Body matches string"] = responseBody.has("string_you_want_to_search");


11. The body converts a JSON object xml

var jsonObject = xml2Json(responseBody);

12. Check a string response is equal to

tests["Body is correct"] = responseBody === "response_body_string";


13. Check the value JSON

var data = JSON.parse(responseBody);
tests["Your test name"] = data.value === 100;


14. The present content type (not case sensitive check)

tests["Content-Type is present"] = postman.getResponseHeader("Content-Type");

15. The present content type (case sensitive)

tests["Content-Type is present"] = responseHeaders.hasOwnProperty("Content-Type");

 

16. Response time is within a specific range (including lower and upper limits)


tests["Response time is acceptable"] = _.inRange(responseTime, 100, 1001);

17. The code includes a string

tests["Status code name has string"] = responseCode.name.has("Created");


18. A successful POST request status code

tests["Successful POST request"] = responseCode.code === 201 || responseCode.code === 202;

19. JSON data for TinyValidator

var schema = {
"items": {
"type": "boolean"
}
};

was data1 = [true, false];

was data2 = [true, 123];

tests["Valid Data1"] = tv4.validate(data1, schema);

tests["Valid Data2"] = tv4.validate(data2, schema);

console.log("Validation failed: ", tv4.error);

20. The encoded data decoding base64

var intermediate,
base64Content,
rawContent = base64Content.slice('data:application/octet-stream;base64,'.length);


intermediate = CryptoJS.enc.Base64.parse(base64content);
tests["Contents are valid"] = CryptoJS.enc.Utf8.stringify(intermediate);

Reference website:
https://blog.csdn.net/weixin_34407348/article/details/93331261
https://blog.csdn.net/qq1124794084/article/details/78049456
https://www.jianshu.com/p/61cfcb436ee4 Postman Manual 4 - API test

Guess you like

Origin www.cnblogs.com/rr1031/p/11304850.html