Automate the API testing using Postman

Automate the API testing using Postman

tudip-logo

Tudip

14 April 2020

Getting ready to release a product and not sure about how to conduct regression testing for APIs? This blog is for you.

Let’s have a basic outline of the terminology used here.

API Testing

API testing is a type of software testing that involves the validation of APIs. The purpose of API testing is to check the functionality, performance, and reliability. It only concentrates on the business logic layer of the software architecture.

Automation

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

Postman

Postman is an API development tool that is used to send and receive API requests. It has the feature to make various types of HTTP requests, saving the environmental variables, and to store the API response. It is also used for API automation.

  • Pre-Request Script:
    Postman allows users to write a script to manipulate the data being sent with the request. 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:
    The environment in postman is a set of key-value pairs which is used to set dynamic values

Steps to be followed to add test cases in postman

  1. Open postman, click on the new button on the top left corner.
  2. Select collection, give a proper name to the collection.
  3. Repeat step 1.
  4. Select Request, provide a name to the request, scroll down and select the collection created in step 2. Click on save to the collection button.
  5. A new tab will open to send the request. Select the request method from the drop-down provided before to the request URL box.
  6. Add the URL into the URL box. Click on the Send button by setting the required parameters required for the request.
  7. Add the required test cases in the tests tab.
  8. Store the request into the collection.

There are two ways to automate the test cases written in postman. They are:

  1. Newman
  2. Postman monitor

Before going to start the process ensure that you have a Postman API key. If you don’t have one, get it by logging into the postman account and follow the below steps.

  • Visit https://www.postman.com/.
  • Click on the dashboard, It will list all the workspaces created by you.
  • Click on My Workspace and click on the integrations tab. It will list all the integrations that have been added already.
  • To add any new integration, click on Browse Integrations. It will list all the several types of integrations.
  • Select the postman API and click on Generate API Key.
  • Provide a name to the API key. Copy the generated key. Store the generated key somewhere for further references. You can access the generated unencrypted key once after generating it.

Newman

Newman is a Node.js package which is a collection runner for the postman. It allows you to run the postman collection directly from the command-line.

Steps to run the postman collection in Newman:

  • Install node.js with version >= 6.
  • Install Newman with the following command.
    npm install -g newman
    
  • Login into the postman and get the URL of the collection by navigating to My Workspace and click on the collection and select the specific collection, store the collection ID from the URL. This is the primary reference id for the collection.
    The URL of the collection will be in the below format:

    https://api.getpostman.com/collections/{{stored_collection_id}}?apikey={{api_key}}
  • 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}}
  • 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 all the success and failed test cases that were added to the requests in the collection.

Please find the snapshots below for your reference.

postman-2

 

The detailed report of the executed test cases will be displayed in table format along with the time taken to run execute the test cases 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, as it is executed through the terminal. The drawback of this mode is, we can’t send the alerts directly. It should be integrated with any third-party software to send the alerts of the execution.

Postman monitor

Postman monitor is similar to the postman collection runner. It monitors the health and performance of the API. It is based on the collections and executes the test cases added to the request in the collection. It will display the graph with the failed test cases and send notifications to the integrated platforms.

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

  • Postman App
  • Postman web

Postman App:

  • In the postman App, click on the ellipsis (…) beside the collection you want to monitor.
  • Select monitor collection.
  • Fill all the required fields like name, version, environment, monitor run frequency, and select the preferred region to monitor the results.
  • Click on the create button.

One can also create a monitor by clicking on the new button.

Postman Web:

  • Login to the postman dashboard
  • Select your workspace
  • Select the monitors tab and click on ‘Monitor a collection in this workspace’.
  • Fill 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.

Now the monitor is ready to execute the test cases added to the requests in the collection. To execute the test cases click on the run button in the top right corner of the screen. You will see a graph with respect to the number of failed test cases. Scroll below to verify the details of the test run. Failed test cases will be highlighted in red. This monitor can also be executed with the REST API call with the below endpoint. The request will wait until the execution of the monitor and responds with the run result. This will be very useful in the CI/CD pipeline process.

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

postman-4-1024x460

postman-5-1024x492

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

Conclusion

API plays a vital role in the development of any application. It is necessary to monitor the health and performance of the API. Regression testing should be done at least once a day. This blog will be useful to automate the testing process which saves time and helps to monitor the performance.

search
Blog Categories
Request a quote