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.
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.
You can download the postman from their official website https://www.postman.com/downloads/ 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.
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
Step-2: In this Tests tab enter your JavaScript code manually or select Snippets next to the code editor.
Step-3: Send the request, your test will be executed after the response is received from the 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
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).
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.
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.
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.
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.
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.
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.