API Response Validation using Postman
Tags: apitesting, postman, testscript
Published: 2021-06-20 18:15:08

In this article, we will learn how to validate the response by writing test scripts and assertions for testing API using postman.

Postman is an excellent utility tool for creating integration tests for rest API endpoints because it allows you to write test scripts and add assertions. We can determine if a test passes or fails based on the output of these assertions.

#Writing tests
In your requests and collections, you can use the Tests tab to write tests that will run when Postman receives a response from the API you requested. For each request, you can add as many tests as you need. When you add tests to a Collection, they will execute after each request inside it.
We can enter the script manually or use the Snippets, which is available on the right side of the code editor
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pp0xet76o0qk3ii3ecgu.png)
 
 

#Basic test syntax 

The pm.* API is used to write your test under the Tests tab in Postman. The pm.test() method accepts 2 parameters.
1.	The first parameter is the name of the test(as a string), it's important to give your test a descriptive name so that you can provide more information if it fails. 
2.	The second parameter is a function that will pass or fail the test if any assertions within your function pass/fail. The function returns a boolean (true or false) 

Postman uses the Chai assertion library for creating assertions. It’s based on the BDD style of assertions which make it highly readable and understandable

#Test results
After you execute the request with the test scripts, go to the Tests tab in the response viewer. It will display a list of tests with the results passed or failed. A boolean which returns true is marked as passed while a boolean which returns false is marked as failed.

#Writing test scripts

Scenario 01: Verify the status code is 200
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jse3t0kg0lwmwco7obs0.png)

Scenario 02: Verify status code contains a string. Example: 201 (created), 200(OK) 
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nugnkmwmeewe9sjixu33.png)
 
Scenario 03: Verify the status code from a list of expected status code (include them all in an array and use one of)
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bscgvevnkplo25rry4f8.png)

Scenario 04: Verify the response time is below 1000ms and above 200ms 
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/crzttb808hrnofcj1n2u.png)

Scenario 05: Verify the presence of header and the value.   
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z7oiole9996yqkvwojnz.png)
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lj2cwhytq90g986nz5gv.png)

Scenario 06: Verify the properties of the objects in response body

The below test include multiple assertions as part of a single test, Even if one assertions fails, the test as a whole will fail. All assertions must be successful for the test to pass
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ekf708ehbcoze2409f3t.png)

Scenario 07: Verify the values in the response body  
Response Body
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mjwo5n6jjr7541qnjakw.png)
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wpqeedzkfqcqfxx1w6gr.png)

Scenario 08: Verify the response value against a variable  

Setup variable in Pre-request Script 
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fiyez9ujq3ah1pgatgmo.png)
ResponseBody
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qikxsqgsyrwmixh4p8r4.png)
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kclz37e39dghq6o7ci68.png)

Scenario 09 : Verify the current Environment   
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q8225uvlxomtposz7k8j.png)

Scenario 10 : Verify the response is valid and has a body and the bodytype is JSON

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7rxdw22b0q1vpjwi7n4d.png)

Scenario 11 : Verify the data type of the response  
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7owji2n7y2ltvdupa6bt.png)
 

#Conclusion
This article aims at showing you how to utilize Postman to write automated tests for your APIs which will allow you to bridge the gap between development and quality assurance, and also minimize the surface area of bugs in your API.