Pm.expect vs pm.response

My question:
What are the differences between pm.expect vs pm.response? Im new to testing and postman. Thanks

Hi @dbatu

pm. is the Postman API used to interact with the built-in set of commands.

pm.response is used to interact with the response that is received back from the server, so for example you could parse the response like this to get the JSON response;

const response = pm.response.json();

or if the response was formatted as test you could use;

const response = pm.response.text();

Postman now also has a BDD framework called Chai JS built into their API.
โ€œExpectโ€ is part of the Chai JS Framework and is used to build your assertions when checking and testing to see if things are as expected.
It is accessed the same way, using the pm. prefix.

Here is a link to the Chai JS Framework website;

Thank you for the answer. My confusion comes from this:

pm.response.to.have.status(200);

Above code checks the value of an attribute of the response as far as I understand.
Now this code under checks the value of a given name for the JSON object of the response:

const responseJson = pm.response.json();
pm.expect(responseJson.success).to.equal(true);

Does the โ€œExpectโ€ work with JSON objects and allow to access to what is inaccesible through pm.responseโ€™s methods?

Both of the following statements do the same thing.

pm.test("Status code is 200", function () {
    pm.response.to.have.status(200);
});

if(pm.response.code === 200){
    console.log("pass");
}else{
    console.log("fail");
}

The difference is that the .to.have, the expect etc. are making use of the built-in BDD framework (Chai JS).

The BDD framework is an easy to read way of asserting that things are as you expect them to be, for example;

pm.test("Check response id exists", function () {
    pm.expect(pm.response.id).to.exist;
});
2 Likes