SDET Unicorns

Creating Postman Tests

Postman Tests

Table of Content

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')
});

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! 

My Resources

Thrive Suite: The all-in-one WordPress theme I used to create my blog.
Jasper AI: My favorite AI writing tool.
Surfer SEO:  A tool to help you rank your content on the first page of Google.

Write Content Faster

5/5

Write blog posts that rank for affiliate terms!

Join our mailing list

Unlock the Secrets to Becoming a SDET Pro in the Industry!

Stay ahead of the curve! Join our mailing list for exclusive insights, tips, and the latest industry updates delivered straight to your inbox

Table of Content

Related Post

7 Principles of Software Testing

7 Principles of Software Testing with Examples

Software testing plays a crucial role in the software development life cycle as it helps ensure the quality, reliability, and performance of the final product. Software testing is not just about finding defects; it also provides valuable insights into the overall quality of the software. By conducting effective software testing, it shows how well the application behaves in different scenarios and whether it meets user expectations.

Read More »
Differences Between CI/CD and DevOps

Key Differences Between CI/CD and DevOps

Software development is far from a simple journey that involves merely executing code and launching applications. It often presents challenges, as it’s rare to get everything right from the start. Each time you implement new or frequent code changes, you inadvertently introduce potential issues that can be time-consuming to resolve. This complexity necessitates a structured approach to ensure smooth production.

Read More »