How to build Web API testing case via Postman

## Introduce

Using APIs to communicate(exchange data) between applications was a easy ways in modern system nowadays. We shall write correspond testing case for verifying and make sure that the programs’ logic meet the requirement(s) for making the application more reliable and reduce bugs before we deploying to the production environment.

To perform testing on Web API, it was not readily compared to console application, we need to simulate a request with format content of Http header and body. Luckily for us, we have some toolkit(s) called Postman that handle those time-consuming works for us, then we could more focus on writing main testing logic/flow.

## Begin with Postman

Let’s start creating first request for API. To demonstrate how easily to begin request using Postman, we have simply write one HttpGet API so called “welcome” that return welcome string from API:

(1) Select “Get”

(2) Input target “Url” **

(3) Press the “Send” button to make a request

(4) Lookup the response windows and watch the response message and Http response status code

** We could define environment variable(s) inside the postman, more detail please refer to reference section

Next, if everything were fine, we could start to write our first test case for “welcome” API:

(1) Switch to “tests” tab

(2) Write down assert statements

(2)-A Should receive Http 200 status code

(2)-B Response body should contains message of key word “Welcome”

(3) Press the “Send” button to make a request

(4) Go to the response windows, switch to “Test Result” and find out that two assert statements were *PASS* or *fail*

Now, we have built up our first test case through the postman. Someone might have question that if we want to create testing case with more complicated scenario(multiple requests within)? How to achieve it?

Below we give a example that we created “Collection” inside with multiple requests in sequence to show how to make it:

(1) First create new collection for this simple test case

(2) Create first request called Push

(2)-A Setup request’s body

(2)-B Setup request’s Pre-request Script ***

(2)-C Setup request’s test assert statement

*** You could write the custom javascript to modify the request body message before the request was sent

(2)-A Push Body
(2)-B Push Pre-request Script
(2)-C Push tests

(3) Create second request called Result

(3)-A Setup request’s body

(3)-B Optional (not necessary, according the requirement)

(3)-C Setup request’s test assert statement

(3)-A Result Body
(3)-C Result Tests

(4) Start collection runner, Press “Runner” will pop-up Collection Runner windows

(4)-A Select target collection

(4)-B Choose environment profile

(4)-C Setup latency delay for reach request

(4)-D Fire the test, and you will see the result of individual request

## Beyond the story

Postman was powerful and provide lots of functions for us to interactive with web APIs, we could do more research on it’s community. Also we could download the command line toolkit “newman” that help us integrate into CI/CD process and ability to generate report as well:)

## Reference

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store