Automate API Tests with Postman

Automate API Tests with Postman

Introduction

Application Programming Interface, is used to stabilize the interaction between two different applications. This article will provide you with a basic idea of how to do the API test automation.

Prerequisites

In this article we will talk about RestAPIs as opposed to SOAP, as RESTful APIs are widely used in the industry.

Before moving to more details about API testing automation we should have a basic idea of RestAPI and Postman. Please refer to the following links to obtain a basic idea:

Postman is one of the best tools for API testing automation and makes it easy to develop and test APIs. We can create and save simple and complex HTTP requests, as well as read their responses. Also using postman, we can automate many types of tests including unit tests, functional tests, integration tests, end-to-end tests, regression tests, etc.

Postman Collection 

Postman Collections are just a group of pre-built API requests. Commonly, a collection represents a set of API calls of one Service. Collections can be easily exported and shared with others. We can create subfolders inside collections group API calls based on their nature/function.

In Postman, you have to click the collections tab on the sidebar and click the ‘+’ icon next to it to create a new collection.

After creating the folder, we can add any kind of method requests Inside the folders.

Postman Environment

In Postman, environments mean a set of variables that we can use in our postman requests. We can create Environments by clicking on the “+” icon on the left or selecting the “New Environment” selection on the top right.

Once creating the environments, we can add valuables to the currently selected environment.

  • Initial value – Initial value is saved in the environment file.
  • Current value – If we need to change the added value temporarily, we can use the current value for it.

We can also download, share, delete, duplicate, or export the environment.

Define API Request

For this article, we are using the “Puppy Store” RESTful JSON mock API. You can access the API using the following URL. We are going to write tests for the below APIs as well as showcase some of the sample responses returned.

  • Create Puppy

This method creates new puppy records in the database. We need to send the puppy data in the request body in JSON notation. This will save the record in the database and assign a unique “id” for the puppy.

URL: /api/puppy
HTTP Method: POST

  • Get All Puppies

List all puppies from the database.

URL: /api/puppy
HTTP Method: GET

  • Get Puppy by ID

Here we get the puppy data by a puppy “id” unique identifier.

URL: /api/puppy/{puppy_id}

HTTP Method: GET

  • Update Puppy

This will update the puppy record. We need to provide puppy “id” in the URL and send updates in the request body.

URL: /api/puppy/{puppy_id}
HTTP Method: PUT

  • Remove Puppy

This request removes a puppy record from the database. You need to send the existing puppy id in the request URL.

URL: /api/puppy/{puppy_id}
HTTP Method: DELETE

You can go ahead and define these requests in Postman or you can import this collection.

Here I have created the “Puppy Store” Postman Collection to save all API requests related to the puppy store. We have to use our API URL in every API request. This can be repetitive and it’s hard to read long URLs. To mitigate that I have defined a Collection variable called “api_url” and assigned the API URL to it. We can replace the API URL with the “api_url” variable from now on.

Inspect Responses & Identify Testing Scenarios

Before writing any tests, we have to call all the API endpoints and make sure that all the API endpoints are working, and inspect the response structure.

Eg: Create Puppy API

If you select the “Create Puppy” API and call it with the correct data you should receive a successful response from the API with a 201 status code.

Based on the response we know for all success scenarios, we must get a response with a newly created record and a 201 HTTP response code.

As shown in the example above, API responses can be different in different APIs. That is why you need to inspect it and get a good idea about the response before you write the tests.

Writing Tests Scripts

  • Variables

We need variables to store and reuse values in our requests and scripts. This can be useful especially if we are using the same values in multiple places. Variables make our requests more flexible and readable, by abstracting some of the detail away.

Postman stores variables as key-value pairs. You can set variables via the “pm” object.

  • Key – Name of the variable
  • Value – Value of the key

 

Variable

Description

Global

Global variables are available throughout collections, requests, test scripts, and environments.

Environment

Environment variables are available throughout the environments (Ex- QA, Prod, local). Only one environment can be active at a time.

Local

Local variables are temporary, and only accessible in our request scripts.

  • Writing Tests

Postman scripts are instructions written in JavaScript. They can be executed before and after API Requests. Scripts have access to the “pm” object and there you can access many features related to the postman.

  • Pre-request Script
    Execute before an API request is sent. Usually used to access/generate data needed for an API request. You can define Pre-request for Collections as well as Folders.
  • Test Script
    Execute after the API response is received. This is the place in which you can write your tests for the API.

Test: Create Puppy API

We are going to write tests for the “Create Puppy” API based on the following scenarios. You can define tests in postman via “pm.test” function. This function accepts testName and specFunction as arguments. testName should be a meaningful name that describes the test.

  1. Before writing the tests, click on the “Create Puppy” request and click on the Tests tab.

  1. Write a test to verify the response code and click send.

  1. Click on the “Test Results” tab to verify the result.

  1. Write another test to verify the response time (the response time should be less than 0.1 seconds), and click on the “Test Results” tab to verify the results.

  1. Write a test to verify the response format.

Postman uses the “Chai assertions” library for assertions handling under the hood. You can find more information about Chai here.

Pass Data Between Requests

There can be a situation where you might want to send an API request that depends on the previous API response data. E.g. We may need to create a puppy and fetch that newly created puppy by its Id in the next request.

We can use Pre-request Scripts and Tests Scripts to achieve this.

  1. Open “Create Puppy” API. We can save the created Puppy Id to a collection/environment variable.

  1. Access the saved puppy Id and attach in “Get Puppy by ID” request URL.

Run Collections with Runner

We can run the sets of requests using a collection runner with a specified order. The collection runner will log our request test results, and our scripts can pass data between requests.

  • To run a collection, open a collection and click Run.

  • By default, our requests will run in the sequence they are listed in the collection. However, if we need to change the order of execution we can click to the left of each request and drag and move it. We can also deselect the requests by unchecking its box.

  • We can enter the number of iterations our collection needs to run.
  • We can enter an interval delay (milliseconds) between each request.

  • We need to click on the “Run Puppy Store” button to execute the requests.

  • Iteration-wise results are displayed below. We can also filter on each using the Passed and Failed tabs at the top.

  • To view the Summary, click on the “View Summary” button.
  • We can export test results by clicking on the “Export Result” button.

  • Once we export the results, the result is displayed as shown below:

References

Umeshika Herath

Software Quality Engineer

API Tests

Related Blog post

1 thought on “Automate API Tests with Postman

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.