Automate the process of API testing using Postman

Posted By :Nuzhat Siddique |29th March 2022

About to release the product and not sure about how to conduct regression testing for APIs?

Let's have a basic summary of the terminology used here.

 

API Testing

API testing is a type of software testing that involves the validation of APIs and as a part of integration testing to determine if they meet user expectations or not. The purpose is to check the functionality, performance, and reliability. It focuses on the business logic layer of the software architecture.

 

Automation

Automation Testing helps to execute the test cases repeatedly just like regression. This API automation is useful for executing the test cases/APIs quickly during the product release.

 

Postman

Postman is an API development and testing tool that is used to send and receive API requests. It has features to make various types of HTTP requests, save the environment-specific variables, and store the API responses as well. It can also be used for automating API testing and execution.

Pre-Request Script:
Postman allows its users to write a script to manipulate the data that will be sent with the requests. This script will be executed prior to the test script.

Tests:
This is the test editor where the user can configure the test cases being validated after the request is fulfilled.

Environment:
An environment is a set of variables you can use in your Postman requests;It is a set of key-value pairs which is used to set dynamic values

 

Steps to follow to add test cases in postman

Open the Postman tool, click on the new button in the top left corner.

Select the collection option and give a proper name to the collection.

Again click on the new button in the top left corner.

Select HTTP Request, click on the Save button to provide any name to the request, a popup window will open, enter the request name and select the collection created in step 2 from the list and click on the Save button.

Select the request method from the drop-down provided before the request URL field.

Add the URL into the URL field and click on the Send button by setting the required parameters required for the request.

Add the relevant test cases to the tests tab.

Store the request in the collection.

 

There are two ways to automate the API tests in postman. They are:

Newman

Postman monitor

 

Before going to start ensure that you have a Postman API key. If you don't have the key, get it by logging into your postman account by following the below steps.

Visit the link https://www.postman.com/.

Click on the dashboard, It will show all the workspaces created by you.

Click on the My Workspace and click on the Integrations tab. It will list down all the integrations that have already been added.

To add a new integration, click Browse Integrations. It will list the different sorts of integrations.

Select the postman API and click on the option Generate API Key.

Provide a name to the API key and copy the generated key. Then store the generated key somewhere for further reference. You can access the generated unencrypted key whenever needed after generating it once.

 

Newman

 

Newman is a Node.js package that is a command-line collection runner for Postman. It enables you to run and test a Postman collection directly from the command line.

 

Steps to run the postman collection in Newman:

1. Install node.js with version 6 or more.

2. Install Newman with the following command.

npm install -g newman

 

3. Login into the Postman application and get the URL of the collection by navigating to My Workspace, click on the collection and select the specific collection you want, click on three dots and click share collection and then go into the tab ‘Via JSON link' and get the collection id (can get the collection ID from the URL if using postman browser app). This is the collection's principal reference ID.

The URL for the collection will be in the following format:

https://api.getpostman.com/collections/{{stored_collection_id}}?apikey={{api_key}}

 

4. If there is any environment, follow the above step to get the environment ID by navigating to the environments tab. The path to the environment will be in the below format:

https://api.getpostman.com/environments/{{stored_environment_id}}?apikey={{api_key}}

 

5. Use the following command to run the postman collection.

newman run path_to_collection -e path_to_environment

Example:

Newman run https://api.getpostman.com/collections/{{stored_collection_id}}?apikey={{api_key}} -e https://api.getpostman.com/environments/{{stored_environment_id}}?apikey={{api_key}}

postman-1

 

It will list down all the passed and failed API requests that were added to the collection.

Please find the snapshots below for your reference.

postman-2

 

The detailed report of the executed API tests will be displayed in table format along with the time taken to execute the tests and also cause for the failure of the test cases will be detailed here.

postman-3

 

This type of automation is mainly used with CI/CD pipeline, as it is getting executed through the terminal. The drawback of this mode is that we can't send the alerts directly. It should be integrated with any third-party software to send alerts of the execution.

 

Postman monitor

 

Postman monitors are comparable to Postman collection runners. It monitors the health and performance of the API and enabled you to run your collections periodically throughout the day. In short, Postman monitors allow you to schedule your Tests to run automatically at defined intervals.

It is based on the collections and executes the API tests added to the request in the collection. It will display the graph representation with the failed APIs and send notifications to the integrated platforms.

 

There are 2 ways to create a monitor. They are:

1. Postman App

2. Postman web

 

Postman App:

In the Postman Application, click on the ellipsis or 3 dots beside the collection you want to monitor and select monitor collection.

Fill in all the required details like name, version tag, environment, the frequency for monitor run, and select the preferred regions to monitor the results.

Click on the create button.

Can also create a monitor by clicking on the new button from the top left panel above the collection list.

 

Postman Web:

Login to the Postman dashboard

Select your workspace

Select the monitor's tab and click on the option ‘Monitor a collection in this workspace'.

Fill in all the required fields like name, version, environment, monitor run frequency, and select the preferred region to monitor the results.

Click on the Add Monitor button.

 

The monitor is now ready to perform the test cases that have been added to the collection of requests. To execute the test cases click on the run button in the top right corner of the screen or center of the screen if available. You will see a generated graph with respect to the number of failed test cases.

 

Scroll below to verify the details of the test run under the Test Results tab. Failed test cases will get highlighted in red. We can also run/execute this monitor with the REST API call with the below endpoint. The request will wait until the execution of the monitor and will return with the executed results. This will be very useful in the CI/CD process pipeline.

https://api.getpostman.com/monitors/{{monitor_uid}}/run?apikey={{api_key}}

postman-4-1024x460

postman-5-1024x492

 

Click on the Console Log tab to verify the test run logs. It will also log any loggers/messages added to the test cases. It will be helpful for debugging the errors in test cases.

 

Conclusion

API plays a very vital role in the development of any application hence t is necessary to regularly monitor the health and performance of the API.  API Regression testing should be done at least once a week. This blog will be useful to automate


About Author

Nuzhat Siddique

Nuzhat is a highly skilled Quality Analyst with a strong background in both manual and automated testing. She possesses extensive knowledge in manual testing methodologies and excels in all aspects of Test Documentation processes. Her proficiency in using tools such as MySQL, Postman, MongoDB, LambdaTest, and Selenium has enabled her to perform effective application testing. She has successfully completed and delivered software testing projects for various domains, including E-Learning, Healthcare, ETL, and IoT. Her notable contributions include projects such as Konfer and HP1T. Additionally, she have experience in test management and defect tracking systems such as Trello and Jira. Nuzhat also has hands-on experience with test automation tools such as Selenium and Protractor. She is a self-driven and self-motivated person with excellent communication and presentation skills.

Request For Proposal

[contact-form-7 404 "Not Found"]

Ready to innovate ? Let's get in touch

Chat With Us