In this post, we will be creating Postman Tests and take a look at some of the advantages of using Postman Tests.
Advantages of Postman Tests
- We can validate the response of the API to ensure it’s working as expected. So far we have just been making API requests using Postman but how do we know the data being returned is what we want, using tests we can verify the response and the functionalities of our APIs.
- We can perform regression testing to ensure new development changes haven’t broken existing functionality. One of the main purposes of writing these tests is to make sure if something breaks we catch it right away through the automation we have built using these tests without having to do anything manually.
How to create Postman Tests?
We can use the pm.test function to create tests in Postman. Here’s an example where we are verifying the status of the response we got back.
![Postman Tests](https://sdetunicorns.com/wp-content/uploads/2020/12/Screenshot-2020-12-14-190554-1.png)
Postman tests use ChaiJS BDD syntax for test assertions. For those that are not familiar with Chai JS, it’s a JavaScript testing library that allows you to add test assertions. Some examples of that are –
- .to.have
- .to.equal
- .to.not.have
These are quite readable and you easily know what the assertion is trying to verify.
Postman Test to verify response body
In the example below, we created a test to verify the response body by using various Chai assertions –
pm.test("Verify response body", () => { pm.expect(pm.response.json().id).to.equal(pm.variables.get("card_id")) pm.expect(pm.response.json().name).to.not.be.empty; pm.expect(pm.response.json().idLabels).to.be.an('array') });
Check out the video below to learn more about Postman Tests –
To learn about API Test Automation using JavaScript, check out my free tutorial series here –
JavaScript API Test Automation Tutorial Series
I hope this post helped you out, let me know if you have any questions in the comments below!
Happy testing!