Postman is a testing tool that can be used to test API’s and verify API response.
Postman provides us the feature of testing the API both manually and also through automation.
In manual testing we have to provide the API URL, input JSON, API method and execute the API to get the output and then we have to verify the output manually.
In automation testing we have to write scripts for testing the API results and run it to verify results.
Why You Should Automate API Tests
The practice of verifying the output using manual testing becomes a time consuming process when the output JSON is large.
Also the tester has to make sure this API is providing expected output consistently each and every day.
This is a more time consuming process and it eats up the valuable time of the tester each and every day.
To overcome this difficulty “Postman” has introduced the capability of automating the API tests, verifying the API response and catching the bugs as quickly as possible.
5 Steps to Automate API Tests
There are mainly 5 steps that are used in automating API using “Postman”. They are:
- Manually testing the API
- Understand the response returned by the API
- Write the automated test
- Repeat for each endpoint of the API
- Run the collection and verify results
1. Manually testing the API
To start with testing API we have to create a “Collection” in Postman that contains the list of API’s that we have to test.
To learn more about creating collections and adding API requests to it, refer this resource by Postman.
An example of collection is shown in the image below.
In the above image we have a collection named “Test Collection” and we have 3 API requests “Test request 1”, “Test request 2” and “Test request 3”.
In each and every API, we can test it manually by giving the base URL, input JSON, request method(GET,POST,PUT,DELETE) and clicking the “Send” button to call the API and verify the output response.
2. Understand the Response Returned by the API
As part of the API response we have to check the status code of the response and the output JSON we got as part of the response.
Usually the status code that we expect for every API is “200” which is the success status code. Also we will check each and every value returned by the JSON is correct or not.
For example consider the image shown below.
In the above image we can see the status code is 200 and we can see the output response in JSON format.
Here we can check each and every value in the JSON whether it’s correct or not.
3. Write the Automated Test
In order to overcome the difficulty of manual testing we can write automation tests for each and every API request and verify the response.
To do this initially we have to open the “Tests” tab for an API.
The image of the “Tests” tab is shown below.
In the above image we opened the “Tests” tab for the “Test request 1” API. Now we have to write the code for assertion of the API response in the text area provided in the “Tests” tab.
For example we can assert whether the status code is 200 or we can assert each and every value returned by the JSON.
Code written in “Tests” tab is shown in image below.
In the above image we have written two tests one to verify the API response and other to verify the JSON response data.
After writing the automation script click on the “Save” button to save the code and click the “Send” button to call the API and get the API response.
The test results can be seen in the “Test Results” tab as shown in the image below.
In the above image, we can see both the test results got passed for the “Test request 1” API.
4. Repeat for Each Endpoint of the API
In the above step we have written an automation script for one API.
Like that we have to write automation script for all the remaining API’s so that we can run the collection and verify the automation result of all the API’s in the collection.
In the above example we have written automation code for “Test request 1” API.
Similarly we have to write automation code for “Test request 2” and “Test Request 3” API’s.
5. Run the Collection and Verify Results
After defining all API’s with the automation scripts we have to run the collection to verify the automation results.
To run the collection we have to click on the arrow icon in the collections section as shown in the image below highlighted in yellow color.
This opens a popup to run the collection as shown in the image below.
Click on the “Run” button shown in the above image which will show the “Collection Runner” popup as shown in the image below.
Click on the “Run Test Collection” button shown in the above image.
This will run the automation scripts of all the API’s in the collection and provide us the automation results as shown in the image below.
In the above image we can see that 6 scripts got passed and no script failed.
We can see the green colored square box in each and every test which means that each and every test got passed.
If any test fails it will show a red colored square box.
In this way we can create as many collections as we need for our application, run it and verify the results.
Do You Need Expert Help with API Testing?
Do you need help with API testing and automating it using Postman?
You can get in touch with testing experts at Perfomatix who are experienced and skilled in validating various Application Programming Interfaces (APIs).
Drop us a note to set up a meeting with our testing team.