API Automation Test using Postman

Indra A.
5 min readDec 17, 2022

--

Photo by Scott Graham on Unsplash

I still remember when my team was confused to decide the automation tools to use and build our first API automation test. Search on the internet, every articles gave you a bunch of automation tools that was popular and easy to use, maybe. Then we realize, we have to understand our product requirement, know what we need, then create key indicators or criteria to select appropriate tools that match with our product’s feature and able to test all the things that we need. As far as I can remember, the main criteria of selecting automation tools back then was less code and less configuration and I found, Postman.

Postman is an API platform to design, build, use, and test APIs. They provide Tests section where we are able to put scripts (written in JavaScript) to validate the API responses.

In this article, I will try to share my experience, also try to elaborate and give a simple example about how to create automation API using Postman. Let’s take a look to the example below to setup the automation.

Steps to Automate

Create new API Collection

First thing first, we have to create new API collection and add the requests inside. For the example, I will try to use Book Stroe API from DemoQA. The endpoint to tests are get all list of books and get specific book by ISBN.

Example of collection and requests to test

Get All Books is a request to get all list of books, hit the URL https://demoqa.com/BookStore/v1/Books using GET method.

Then Get A Book is a request to get specific book by their ISBN value, hit the URL https://demoqa.com/BookStore/v1/Book?ISBN=<ISBN value> also using GET method.

Add Test Scripts

For API response validation, we will check the status code, response time, response body structure (schema), and the response value. Here is the example scripts in Tests section.

// validate status code is correct (200)
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});

// validate response time is less than 500ms
pm.test("Response time is less than 500ms", function () {
pm.expect(pm.response.responseTime).to.be.below(500);
});

To validate the response schema, we need to define the JSON in schema format.

// JSON schema of Get All Books
var schema = {
"type": "array",
"items": {
"type": "object",
"properties": {
"isbn": {
"type": "string"
},
"title": {
"type": "string"
},
"subTitle": {
"type": "string"
},
"author": {
"type": "string"
},
"publish_date": {
"type": "string"
},
"publisher": {
"type": "string"
},
"pages": {
"type": "number"
},
"description": {
"type": "string"
},
"website": {
"type": "string",
"format": "uri"
}
},
"required": [
"isbn",
"title",
"subTitle",
"author",
"publish_date",
"publisher",
"pages",
"description",
"website"
]
}
};

var jsonResponse = pm.response.json().books;

// validate response body schema
pm.test('Schema is valid', function () {
pm.expect(tv4.validate(jsonResponse, schema)).to.be.true;
});

// set book ISBN and title to collection variables; will be used for Get A Book
pm.collectionVariables.set("isbn_id", jsonResponse[0].isbn);
pm.collectionVariables.set("book_title", jsonResponse[0].title);

As we can see in the scripts above (on the last line), we also set collection variables with the book ISBN and title value. For testing purpose, we don’t want to use same ISBN for every run so we need to get the ISBN value of first book from Get All Books response.

How to use variables in parameters (as well as in header and body request)

To use the isbn_id variable in Get A Book request, define the parameter value with format {{<variable name>}}. In this case, the parameters will be {{isbn_id}}.

Run Test Scripts

After finished create new collection, add the requests to test, and also add the test scripts for each request, then we’re ready to run!

Run the tests by click the Send button for each request. You will be able to see the Test Results including how many validation are passed, failed, or skipped.

Example of full test scripts and the Test Results after click Send Request

But this method will require you to click the button one-by-one for each request. To run all the requests and tests at once, look for Run Collection option besides your collection name.

Example of the result and summary after Run Collection

You will be able to see all the requests were hit and tested one-by-one. The result and summary, including the error if the response is not matched with the expectation, will shown as in the image above.

Key Takeaways

Here I’ve summarized the conclusion from this experience and example of the API automation using Postman.

  1. Choose your automation tools based on what you need. In my opinion, this “need” means what to test, how to test, and how to create the test automation. If you are choosing the tools that matched with your need, then the automation could be powerful.
  2. Postman can be one of your option. From my experience, Postman will give you an experience of easy to use. No need an additional configuration, just download the Postman desktop, define the requests, and add the test scripts in JavaScripts you can test all the endpoint in your services.
  3. Postman also can not be one of your option. In my opinion, the test scripts in Postman is not that flexible. You can not re-use the scripts, including the JSON schema definition, in one request to another. We need to define the scripts again for each request even if the test are the same. The collaboration also needs more costs since we can not use Git to collaborate, Postman requires paid plans to be able collaborate through Postman desktop.
  4. Look for references and do mini-research before decide your automation tools. A lot of automation tools are available nowadays, I’m not sure everyone has a lot of time to try the tools one-by-one until they found tools that matched. Articles of automation tools from others will help you to understand and know the how it works and how to use without try by yourself (more time is needed to try one-by-one).

In my case, Postman is not the option of our automation tools because lack of re-use code/scripts and requires more costs to collaboration. But, Postman is the first tools that I tried for automation testing and helps me to understand better about what to test for API.

References

--

--