DEV Community

Cover image for Creating Postman Tests
Dilpreet Johal
Dilpreet Johal

Posted on

Creating Postman Tests

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

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')
});
Enter fullscreen mode Exit fullscreen mode

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


📧 Subscribe to my mailing list to get access to more content like this
👍 Follow @automationbro on Twitter for the latest updates

...

I love coffees! And, if this post helped you out and you would like to support my work, you can do that by clicking on the button below and buying me a cup of coffee -

Buy me a coffee

You can also support me by liking and sharing this content.

Thanks for reading!

Top comments (0)