How to measure metrics or validate attributes in APIs via Postman

Postman, is an API testing tool that is used by developers and testers to validate all types of APIs. In this blog, we demonstrate how to validate different attributes and parameters of API response with the help of a test and script in Postman.

GraphQL has a role beyond API Query Language- being the backbone of application Integration
background Coditation

How to measure metrics or validate attributes in APIs via Postman

Why postman?

  • It has a simple UI for sending and viewing HTTP requests and responses.
  • One is not required to write any HTTP client network code.
  • It can make different types of HTTP requests like GET, POST, DELETE, UPDATES, PUT, PATCH, etc.
  • It allows users to build collections of APIs and share them among colleagues.
  • It allows writing and automating the tests which help when repetitions or iterations of API testing are required.
  • It allows users to write a Pre-request script.
  • It allows writing multiple tests for a single API.
  • It allows users to validate various attributes using variables and assertions.

Importing and exporting API data, sharing collections, or asking people to contribute to existing collections are some of the advantages which are provided by Postman out of the box.

How to get postman on your machine (windows/mac/Linux)

You can download the postman from their official website based on your operating system. If you are a windows machine user then select the version of the operating system (32-bit/64-bit) and download the application and install it. After installation signup with all the required details, or you can also sign up with a Google account. Also without signup, you can use postman but it is better to create an account. It will help you to save your work in the postman workspace.

Writing tests

Tests ensure that your API is working as per business requirements or your expectation. The purpose of the writing test is to evaluate metrics like Status code, response time, and response header and validate various attributes using variables and assertions methods. You can write and run tests in Postman for each request as well as collection.

Here I will help out to write your first test in postman, follow the below steps to write the test in postman.

Step-1: Open the request and Click on the Tests tab

Open request - Postman API

Step-2: In this Tests tab enter your JavaScript code manually or select Snippets next to the code editor.

Javascript code - Postman API

Step-3: Send the request, your test will be executed after the response is received from the API.

Test executed - Postman API

You can see your test output results under the Test Results tab at the bottom side.

Now let’s understand the code we have written in test

Test output - Postman API

pm.test -> This is a function for writing test specifications, It works in a non-blocking way in case of errors and can include multiple related assertions.

1st parameter - > test name as a string, In the above code it is “Status code is 200” You can give any test name as you wish

2nd parameter - > callback function (called when case assertion gets finished). A callback function is also known as a higher-order function, this function is used to pass to another function.

pm.response - > It is the response assertion API and can make assertions on the response object( status code, headers, body).

Measuring metrics

We have written this code for validating the status code of the API response whether it is 200 (Success) or not. You can see the test result is passed because the status is 200 which is in the response of API. If the API response is not 200 (success) it may be like 500-Internal server error, 404- Not found, etc and the get test failed. You can see in the image the test failed saying that the expected status code was 200 but got 404.

Status code 404 - Postman API

Similarly, we can measure the response time of API with certain conditions. In the below example, we have put the condition that response time should be below 2000ms, If the required condition is satisfied then the test gets passed otherwise it fails.

Response time below 2000ms - Postman API

Validating attributes or parameters from the response body

API response bodies have different attributes If we want to check whether the response body contains the correct attribute which we required in our expectation. We need to add tests with assertion conditions. In the example I have put two conditions, one is the car registration number should be 900 and the second one franchise should be HONDA. After sending a request It will store the JSON body response in one variable that JSON data and then validate the assertion conditions. If all conditions are satisfying then pass the test otherwise it fails. We can put multiple assertion conditions in a single test to validate different attributes from the JSON response body.

Validating attributes - Postman API

Writing script

The script is nothing but multiple tests in a single request or collection file. If you want to execute your code before the request runs you can use a pre-pre-request script. Also, your test can include multiple assertions, If a single assertion fails then the whole test fails. To pass the test all assertions must be passed for a single test. You can see the number of tests Passed, Skipped, and Failed in the test results. In the below example, there are four tests, out of four 3 passes and one fail. 

Script writing - Postman API

Running collection

API collection is known to all, It is nothing but a list of APIs that are made up of common endpoints. As I mentioned earlier also we can add scripts to single API requests or collections of API. When we run the collection all test scripts related to the collection will be executed after every request in the collection. Users should be able to see the test run results, which comprises details of the environment, iteration, duration, number of tests, Average response time, and test which are passed or failed, or skipped. We can run collections as many times with different iterations which will be helpful in our performance testing.

Running collection - Postman API

We hope this article will help you when writing your first test script to substantiate attributes of the API response body as well as running the API collection in postman.

Hi, I am Umesh Wagh, a quality assurance engineer. I enjoy the opportunity to learn something new in this tech era.

Want to receive update about our upcoming podcast?

Thanks for joining our newsletter.
Oops! Something went wrong.