The pm object is the JavaScript object that the Postman provides that gives us access to data from requests and responses.
It also lets us to access and manipulate the variables and cookes.
The test method is an asynchrnous method that we can use to set up each check that you want to do.
This method takes 2 arguments.
The first argument is the name of the test as String
The second argument needs to be a function that does the actual check that we are interested in
pm.test("Understanding the test syntax - 1", function() {
console.log("Traditional function")
// Ideally we should be writing the check
});
pm.test("Understanding the test syntax-2", () => {
console.log("Arrow function")
});
Using Chai assertions in postman
Assertions will check the data/response/anything which you want to vlidate.
If assertion fails, tests fail.
Java Script has an assertion library called as Chai Assertion Library & postman uses that Refer Here
The pm has expect function which can be used or chai module can be imported
const chai = require('chai');
pm.test("Understanding Response", () => {
chai.assert(pm.response.code === 200, "Response code should be 200");
chai.assert(pm.response.status === "OK", "Status should be OK")
pm.expect(pm.response.code).to.eql(200, "Response code should be 200");
pm.expect(pm.response.status).to.eql("OK", "Status should be OK")
});
Postman allows us to use the following external libraries Refer Here
pm.test("Understanding the test syntax - 1", function() {
console.log("Traditional function")
// Ideally we should be writing the check
});
pm.test("Understanding the test syntax - 2", () => {
console.log("Arrow function")
});
const chai = require('chai');
pm.test("Understanding Response", () => {
chai.assert(pm.response.code === 200, "Response code should be 200");
chai.assert(pm.response.status === "OK", "Status should be OK")
pm.expect(pm.response.code).to.eql(200, "Response code should be 200");
pm.expect(pm.response.status).to.eql("OK", "Status should be OK")
});
pm.test("Name should be Luke Skywalker", () => {
const responseBodyInJson = pm.response.json();
pm.expect(responseBodyInJson.name).to.be("Luke Skywalker");
})
Exercises:
Find out the differences between const, let and var and when to use what
when to use require or import and how to use them.