Postman: Automating Rest APIs

Deepak Attri
Geek Culture
Published in
5 min readMay 3, 2021

Postman is an excellent tool for testing APIs. It can be used by backend developers, web developers, mobile developers, and QA testers. Postman is not only used to test APIs manually but also is a great automation tool.

You can write automation scripts using JavaScript and write assertions in chai.js, which is a BDD/TDD assertion library. Postman allows running tests headlessly with Newman and adds to your build pipeline. You can also export test reports using Junit and Newman. The scope of this article is limited to writing test scripts only.

Writing Test Scripts

You can add Javascript code in two places in Postman.

  1. Pre-Request Script: You can use pre-request scripts in Postman to execute JavaScript before a request runs. Under the Pre-Request Script tab you can add JavaScript code to set variable values, parameters, headers and request body data.
  2. Test Script: You can use the Test tab for writing test scripts. Your test scripts can use dynamic variables, carry out test assertions on response data, and pass data between requests. The test script will execute after the request runs. You will be able to see the output in the Test Results tab alongside the response data.

You can add pre-request and test scripts to a collection, a folder, a request within a collection, or a request not saved to a collection.

In this article we will discuss how we can automate POST, GET, PUT and DELETE APIs. Basic JavaScript knowledge is required for automating in Postman.

POST Request

We will consider the following request and response while automating.

End Point:

http://some-web-address/inventory/car

Request Body:

Response Body:

The API needs a different id(Integer), name(String), displayName(String) every time the request is triggered. Let us see how we can pass random values to these fields.

We can generate random/dynamic data in requests using the following functions/libraries.

  1. faker.js : Faker.js is a JavaScript library that exposes functions that generate random data on run time.
  2. Math.random(): This method returns a pseudorandom double greater than or equal to 0.0 and less than 1.0.
  3. JavaScript Lodash _ : _.random (min,max) generates a random integer between a given range.

Pre-Request Script

Pre-request Script

In the above script, we are first using Math.random() to generate random strings viz. name and displayName. Next, we create an environment variable cargroupName and set its value to name and variable cargroupDisplayName and set its value to displayName. For setting the value of environment variable id we are using the function $randomInt provided by faker.js .

Alternatively, we can use pm.environment.set(“id”,_.random(1000,10000)) for setting the value of environment variable id. Its returns a value between 1000 and 10000.

All the values set in environment variables will be used in the request body.

Request Body
Environment Variables

Before execution Pre-request script will run and the variable values will be set. These values will be used in the request body. Now let us see how the test script will look like.

Test Script And Response
Passed Test

We have written assertions using chai.js. The above test script contains three tests defined by pm.test(). We can also log onto the postman console using console.log . Let's see how a failed test looks like

Failed Test

GET Request:

Now we want to read the created resource using a GET request and the id should be passed from the POST response. We simply need to put the id in the API endpoint since it's already set as an environment variable.

End Point:

http://some-web-address/inventory/car/{{id}}

Response and Test Script

Here the after the response is received from GET request, it is updated and the priority field in the JSON is updated to 10. The updated JSON is saved to the environment variable to use in PUT Request.

PUT Request:

Environment variable savedData can be used directly in the PUT request and you can see in the response that the priority field is changed to 10.

End Point:

http://some-web-address/inventory/car/{{id}}

Request Body And Response

DELETE Request:

Again we can call the delete request with the environment variable id to perform the delete operation.

End Point:

http://some-web-address/inventory/car/{{id}}

Delete Request

GET request to check the status after delete is performed. Here you can see that the status is changed to 0.

Get Request After Delete

Executing Collection

Postman allows us to run the entire collection. The APIs can be arranged in the required sequence and upon executing the collection the APIs will run in the defined order.

Run Sequence of APIs In Collection
Run Collection Window

Here you can select which APIs to include, define the number of Iterations, save responses and other things. Click on Run Automation Example button to run the collection. Automation Example is the name of the collection.

Running a Collection

Pros and Cons of Automation in Postman

Pros:

  1. You do not need to create or maintain an automation framework.
  2. Easy to use, no code required to connect to REST client or generating reports.
  3. Code snippets provided makes script development fast.

Cons:

  1. Test Scripts/pre-request scripts can not be reused and hence no code reusability.
  2. Cannot integrate with reporting frameworks such as allure, extent reports etc.
  3. Does not provide support to connect to a database or excel file.
  4. Difficult to test scripts on multiple data sets.

Postman is a great tool if you want to automate CRUD APIs for small microservices since it is simple and you do not need to create any framework.

Full projects and more repositories can be found on my github.

--

--

Deepak Attri
Geek Culture

Quality Assurance Engineer | JAVA | API Testing | Mobile Testing | Web Testing | Performance Testing