- RISE FROM THE ASHES – Sanjib Nandi - November 1, 2021
- साथ में मेरे दोस्त खड़े थे - August 1, 2021
- BenchmarkDotNet: Advanced Features - June 20, 2021
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 –
- Create sample GET request in Postman
- Create sample POST request in Postman
- Create Test Suite
- Create environment variables
- Write automated test cases
- Run tests using Collection Runner
- 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.
1. 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..
This will return the response as shown below, where
- #1 shows the response body,
- #2 is about how do you want to see it. Pretty is beautified version of JSON,
- #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 is the Response status code.
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.
and we can see the similar response screen below
Important – Since 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.
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.
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.
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.
To enable this variable to use, you need to select Environment from the drop down
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.
It is again in 3 steps –
- Go to Tests tab
- 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.
- Hit send and verify the test results tab in the response section.
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.
Clicking run will take you to this launch Collection Runner and all fields are here for test configuration and self-explanatory.
And on this dashboard, you can see the results. I have intentionally failed few of them.
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.
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 –