Automated API Testing with Postman

The old days are gone but still,  Postman is our favorite because it has changed its job to API testing. Don’t be confused --  I am talking about the API testing tool Postman. It is free to download.
 
In this article, I assume that you know the definition of Web API and the basics 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 experiments.
 

Create Sample GET Request in Postman

 
Create new Request by clicking + New from top left corner.
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..
 

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. 
  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.
 
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.
 

Create Sample POST Request in Postman

 
This is also a similar process as GET with some additions 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.
 
 
 
Create POST Request and we can see a similar response screen below
 
 
 
Request and Response Body
 
ImportantSince this is a demo service, it will not store your data. Id generated canNOT be used to retrieve the user.
 
SO far  we finished the basic steps in Postman. Now let us move to Automated testing.
 

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.
 
 
 
It gives the option to add folder/(s) and you can group them as per your need. I have put my example above.
 

Create Environment Variable

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

Set up the Environment and Environment

 
On the next pop up, you can add the environment first and then as many variables as you want.
 
In my case, I have just one variable in my Experiment Environment.
 
 

Creating/ Updating Environment and Variables

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

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 the scope of Collection but Global Variables can be used across collections.
 
Again, hit send and verify the response and status code.
 

Write automated test cases

 
Now click the tests tab in the Request section.
 
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.
Test Scripts in Postman
 

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 the highlighted button shown below and hit the Run button.
 
 
Running collection in one go
 
Clicking run will take you to this launch Collection Runner and all fields are here for test configuration and are self-explanatory.
 
 

Collection Runner

 
And on this dashboard, you can see the results. I have intentionally failed a few of them.
 
 
 
Automated Test Results and Dashboard
 
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.
 
 
Export and Import
 
For your experiment, I have exported the environment and collection here.
 
I hope this article gave you an easy understanding of the automated testing of Web APIs. Originally published on the taagung.
 
References
  • https://reqres.in/
  • https://learning.postman.com/docs/postman/scripts/test-examples/


Similar Articles