postman pm test code example
Example 1: postman test script get request code
tests["Status code is 200"] = responseCode.code === 200;
console.log(tests["Status code is 200"] = responseCode.code != 200)
Example 2: test snippets in postman
I mostly use preInstalled js testScripts in postman
in Tests tab. Basically test will execute after
response received for verification. So when I send
the request by clicking SEND postman runs the test
script and after response gives me test result
Some of the snippets I use:
Get an environment variable
Set an environment variable
Clear a environment variable
Clear a global veriable
Get a global variable
Set a global variable
Send a request
Response convert xml to json
Response time is less than
etc.
Example:
pm.test("Status test", function () {
pm.response.to.have.status(200);
});
pm.test("response must be valid and have a body", function () {
pm.response.to.be.ok;
pm.response.to.be.withBody;
pm.response.to.be.json;
});