postman test example
Example 1: how do you test 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;
});
Example 2: .to.be.true; in postman
var schema = {
"items": {
"type": "boolean"
}
};
var data1 = [true, false];
var data2 = [true, 123];
pm.test('Schema is valid', function() {
pm.expect(tv4.validate(data1, schema)).to.be.true;
pm.expect(tv4.validate(data2, schema)).to.be.true;
});