As a tester, I have been exploring different tools to convert manual testing into automation process. So, next time I don’t have to execute the same things again manually.
Over the period of last few months I have been exploring and experimenting with various testing tools and frameworks. And during that period I found that Postman is one versatile tool. It allows me to not just check the API responses but it has the ability to write test cases. I can write test cases to check whether the API is behaving as they are expected or not.
At our company developers follow swagger to write documentation of APIs. So, it’s easy for a tester to get a idea what will be the response in the different scenarios.
In some cases, response can be 200 status code or sometimes it cane be 400, 404 or 500 status code with a proper messages.
Now that I have given a little background on what I do and why I do. It’s time deep dive into Postman and testing using postman.
Lets begin with why one should use Postman for API Testing
- Very easy to create a Test case
- History is well maintained with proper folder structure
- Reusable Components using Environment
- Postman can also Manage workspace
- Effortless Configuration with Jenkins
Lets begin with Quick tour for Postman
How to Install Postman ?
Click on here to download the postman.
After downloading, install it and after installation the first look will be as below
Once you have successfully completed the installation you would need to Signup or Sign in into the application to access it.
How to setup collection and request?
Step 1: Click the request button in the dialog shown below. As you are entering the postman for fist time you would be presented with this wizard. But if you are a returning user you would find a button “New” on the top left corner of the system to add new request.
Step 2: Enter an appropriate request name. Ideally you like to use a common naming convention. For e.g. ; Get company by ID
Step 3: If you see at the bottom part of the form you will see a clickable textual button that say “Create collection”. Click that button to create the collection under which you could group your request. Ideally it would be the project name and underneath that you may create folders if you want to group your requests by the features that you are working on.
Step 4: If you want you can enter a brief description and save the request.
Once you have completed the above step you would be able to see something like shown in the image below.
Now you need to enter the request/API URL here in my case I have this demo
URL : https://jsonplaceholder.typicode.com/posts
Now Before entering request URL ,one should have knowledge about basic [HTTP methods]. You can learn about it from link below
(http://www.restapitutorial.com/lessons/httpmethods.html).
After entering URL and setting up the request headers,click on the Send button to execute the API call. If you have set the request header and the passed the required parameters the post man will display the response of the request you made as below. If there’s something missing the system will throw error.
Now we are going to write a simple test for this request URL .For that we need Postman Sandbox.
What is Postman Sandbox?
The Postman Sandbox is a JavaScript execution environment that is available to you while writing pre-request scripts and test scripts for requests you make via postman.
Here is an example test case that I have used to demonstrate the testing.
// example using pm.response.to.have
pm.test("response is ok", function () {
pm.response.to.have.status(200);
});
// example using response assertions
pm.test("response should be okay to process", function () {
pm.response.to.not.be.error;
pm.response.to.have.jsonBody("");
pm.response.to.not.have.jsonBody("error");
});
// example using pm.response.to.be*
pm.test("response must be valid and have a body", function () {
// assert that the status code is 200
pm.response.to.be.ok; // info, success, redirection, clientError, serverError, are other variants
// assert that the response has a valid JSON body
pm.response.to.be.withBody;
pm.response.to.be.json; // this assertion also checks if a body exists, so the above check is not needed
});
This piece of code will check whether the request URL is giving 200 status code or not. The request response will either pass or fail the written test cases. Postman will display the tests results like below.
Now in a real life it would be highly impractical to run the test for each and every request in the system. Postmanhave the feature called “Test runner” with help of Test runner we can write and test suite that wold test all the request and share with us the result of the test. It will run all request in particular collection which we select.
Writing your first postman “Test runner”
Step 1: Click the gear icon in the top right corner highlighted in the image below. It will present you with the dialog to create the environment.
Step 2: Click the add button to create a new environment.
Step 3: Name the environment and add the variable that you want to access in that environment. The variables gives you the ability to assign dynamic values in the request URL you make. Click here to know more about variables
In the from above you can see that I have assigned URL to my host variable so that I do not have to enter the base URL for my request every time I create a new request in my collection.
The image below show that I selected the environment for my URL request and I am using the host variable that I defined above to create my request URL under the collection of json schema data validator.
Step 4: Let’s write a code to set our title variable defined in our json schema environment dynamically from the response of our request.
Step 5: For demo purpose I am using the demo Test runner suite given by postman. To run the test please follow the directions step wise as in the images below.
We are a team of expert developers, testers and business consultants who strive to deliver nothing but the best. Planning to build a completely secure and efficient React app? 'Hire React Developers'.
Image 1: Accessing the test runner wizard.
Image 2: Test runner wizard popup.
Image 3: Setting up the Test runner.
Image 4: Test results
Above image displays the result of the entire collection with number of test cases passed and number of test cases failed.
Now that I have taken you through the Postman right from installation to creating your first test case, please share your feedback about the blog. Was the blog helpful to you to begin your testing journey in the postman.
If you like us to create Test runners for your projects, web application, mobile applications in postman. Or want to test the wonderful application you have created HIRE US here.
Hope you enjoyed the article!!!
Top 6 React UI Component Libraries and Frameworks in 2020
Read MoreThird Rock Techkno is a leading IT services company. We are a top-ranked web, voice and mobile app development company with over 10 years of experience. Client success forms the core of our value system.
We have expertise in the latest technologies including angular, react native, iOs, Android and more. Third Rock Techkno has developed smart, scalable and innovative solutions for clients across a host of industries.
Our team of dedicated developers combine their knowledge and skills to develop and deliver web and mobile apps that boost business and increase output for our clients.