Automated API Testing with Postman

Atul Sharma

Old days are gone but still, the postman is our favorite because it has changed its job to API testing. Don’t be confused I am talking about API testing tool Postman. It is free to download.

In this article, I assume that you know the definition of Web API and basic of GET POST and other HTTP methods and JSON. Also, I have skipped the download and installation process of the tool.

What are we going to cover in this article –

  1. Create sample GET request in Postman
  2. Create sample POST request in Postman
  3. Create Test Suite
  4. Create environment variables
  5. Write automated test cases
  6. Run tests using Collection Runner
  7. Export and Import Tests/Environment/Collection/Request

For this whole article, I will be using https://reqres.in/ for demo purposes. Though I have taken GET and POST only there are many other methods available for your experiment.

See also  International Yoga Day: You need to know MORE

1.      Create Sample GET Request in Postman

Create new Request by clicking + New from top left corner.

Postman

Select Method as GET and URL as https://reqres.in/api/users/1 to fetch the user with Id = 1 and hit the Send button as shown here 1,2 and 3..

Postman
Create GET Request

This will return the response as shown below, where

  1. #1 shows the response body,
  2. #2 is about how do you want to see it. Pretty is beautified version of JSON,
  3. #3 has header is response. (You can also inspect the header in Request object as well, being GET, we don’t put anything explicitly, but Postman adds some.)
  4. #4 is the Response status code.
Postman
Request and Response body

If GET request supports querystring, we can have them added in Params tab in Request section. This is not applicable here.

2.      Create Sample POST Request in Postman

This is also a similar process as GET with some addition in terms of the request body but based on service documentation, you may have to add authorization, Headers, etc. But for this demo, let us simply follow this screenshot and get the response.

Postman
Create POST Request

and we can see the similar response screen below

Postman
Request and Response Body

ImportantSince this is a demo service, so it will not store your data. Id generated can NOT be used to retrieve the user.

Till now we finished the basic steps in Postman. Now let us move to Automated testing.

See also  Dynamics 365 : Many to Many (N:N) Relationship

3.      Create Test Suite in Postman

In our test suite, we can have many services hit for tests. So the test suite is to organize our test cases. And It can be created using New Collection shown in the screenshot, from the left-hand panel of the postman.

Postman
Postman Test Suite with Collection

It gives option to add folder/(s) and you can group them as per your need. I have put my example above.

4.      Create Environment Variable

As we saw here, every time we need to add the same URL to the multiple requests, the only user id is getting changed. So, in such cases, to enable reuse and avoid typos we can create variables by clicking this setting button on the right top corner.

Postman
Set up the Environment and Environment

On the next pop up, You can add environment first and then variables as many as you want.

For my case, I have just one variable in my Experiment Environment.

Postman
Creating/ Updating Environment and Variables

To enable this variable to use, you need to select Environment from the drop down

Postman
Using Environment and Variables

Now, key in the variable name between double curly braces i.e. {{URL}}1  and append the user id. You will get IntelliSense as well.

Variables will have scope of Collection but Global Variables can used across collections.

Again, Hit send and verify the response and status code.

5.      Write automated test cases

Now click the tests tab in Request section.

See also  All Steps about Query Execution in Entity Framework

It is again in 3 steps –

  1. Go to Tests tab
  2. You can take code snippets from the right-side panel and customize it as per your testing scenario. Here basic knowledge of JSON is a must.
  3. Hit send and verify the test results tab in the response section.
Postman
Test Scripts in Postman

6.      Run tests using Collection Runner

In our last step of testing, we are going to use Collection Runner as we may have hundreds of scenarios to test. Manually running them could be difficult.

For this, you can click highlighted button shown below and hit the Run button.

Postman
Running collection in one go

Clicking run will take you to this launch Collection Runner and all fields are here for test configuration and self-explanatory.

Postman
Collection Runner

And on this dashboard, you can see the results. I have intentionally failed few of them.

Postman
Automated Test Results and Dashboard

7.      Export and Import Collections, Request

Unlike this example, you may have thousands of test cases to test and run. You may need that to share with your team as well. For this purpose, you can export (1) and Import(next to New) the collection and start testing from the below screenshot.

Postman
Export and Import

For your experiment, I have exported the environment and collection here.

I hope, this article gave you easy understanding in automated testing of Web APIs.

References –