Postman – your devoted friend in API testing

Some day I was sitting behind my desk at work and suddenly out of nowhere (like always, huh?) one of our project managers appeared with a question:

 – Hey, can you help us with our internal project?
 – Yes, of course, why not? – I responded quickly.

I was at a very beginning of my QA adventure, I didn’t know many things yet but when a well-experienced employee step by you and ask you for help, what would you do?

He introduced me to the project members, they told me about the application and it was really interesting. And then it happened. They told me that application doesn’t have a frontend yet so I have to test it using API… and I asked myself in mind: What is an API? How do I test it? What’s worse, they seem to think like this kind of testing must be obvious to me so they didn’t tell me much about it. In addition to this, I did the worst thing I could ever do in this kind of situation. I was too afraid to ask for help and explanation.

Thankfully they sent me the recommendation which tool is best to use for API testing and also a full documentation(in swagger. Didn’t know that either 🙁 ). This tool was a Postman. At first, I was struggling with it but by trial and error, we started getting together. Now I can only be grateful for this experience because I loved Postman and I’m using it on a daily basis. In this post I’d like to show you why working with Postman is easy and profitable.

Let’s start from the beginning

From the developers description we know, that:

 “Postman is a powerful API testing suite which has become a must-have tool for many developers. We make beautiful products to build amazing APIs and improve developer productivity. Postman is now used by well over a million developers around the world, and this number is rapidly growing. With more products in the pipeline, we aim to provide the most comprehensive API development and testing solution to developers.”

I can totally agree with this description. In an actual version, it’s even more powerful than it was when I started using it. Many of the PRO features are now free to use. And don’t be afraid, it’s not only for developers. Even it’s cool, advanced feature – automated response testing doesn’t require much of a knowledge. It’s just simple and powerful.

Let’s quickly run over its best features!

Sending request

If you install postman for the first time, their team supports you with “Postman Echo” collection. The collection is a set of saved requests (and responses) organized into a logical piece. Postman Echo is made for you to easily jump into API testing with postman with a pre-configured request so you can easily base on it.

If you open Postman Echo collection, then “Request Methods” and then “GET Request” all saved input will appear in the central section of Postman window. Now hit “Send” button.

Congratulations, you send your first request and got first response in Postman. It’s that easy.

Postman get request

POST request is a little more complicated but still easy and logical.

This time you will use your own, custom request. Click on the plus sign to open a new tab, change request type from GET to POST, as a request URL use https://jsonplaceholder.typicode.com/posts.

JSONPlaceholder is a free online REST service that you can use whenever you need some fake data. It’s great for tutorials, faking a server, sharing code examples, …

Now you will need a body for a POST. Click on “Body” under request URL, then change the type to “raw” and “Text” to “JSON”. Finally, paste this request in the editor:

 

{
   "title": "My first POST request",
   "body": "Content of my first POST request!",
   "userId": 789
}

postman post request

And click “Send”. You should receive a response with your added post as a confirmation of the successful request.

Now you should save your request for future use. We will also create a new collection.

Simply click save at the right side of request URL. Saving dialog will appear where you can name your request (i.e. Add post) and create a new collection for this and future request related to this tutorial.

Saving collection

Tests

Next great feature of Postman is automated request tests. You write a test for a request once, and every time a response comes postman automatically test it based on your test cases. I’ll show you some simple test. Add the new custom request to get the post from https://jsonplaceholder.typicode.com/posts with ID = 6 and open tab called “Tests”.

At the right side of postman window you will see a snippets list. It will help you to start with creating your first, second and every next test because they are generic. You just need to adjust them slightly to fulfill your needs.

In our first test we will check if the response to our request returns HTTP status ‘200’, if the user id is 1 and post id is equal to 6, and if response body contains some text.

tests["Status code is 200"] = responseCode.code === 200;

var jsonData = JSON.parse(responseBody);
tests["User ID is 1"] = jsonData.userId === 1;
tests["Post ID is 6"] = jsonData.id === 6;
tests["Body matches string"] = responseBody.has("dolorem eum magni eos aperiam quia");

Now you can just simply hit “Send” button. After response is received you can click on Test tab at the top of response section and check if all our tests passed.

postman passed tests

The last thing I want to show you in this first part of getting familiar with Postman are the environments. I’m sure you have(at least you should have) multiple environments of your application/website/whatever. For example development environment, staging, UAT and so on. In Postman you don’t have to create every request separately for every environment. Maintaining these will be terrible and I guarantee that there will be mistakes and discrepancies eventually. Postman allows you to create environments with variables which you can use in creating requests and tests. For example:

We have 3 environments: dev, staging, and UAT. We would like to test some request on all of these environments. So we create one request with URL constructed like this:

{{url}}/posts/6

Then we create three environments and for each of them we add variable with respective URL, for example:

postman variable configuration

Now you just need to choose environment in the drop-down menu on the right side of the window and Postman will add your defined variable every time you send a request. Now, especially for POST request, you don’t need to worry about changing it for every environment separately.

postman choose environment

Summary

The main purpose of this post wasn’t to make you Postman ninja in 15 minutes but rather to show you how easy and fun is to work with it. Especially knowing that people still use JMeter even for simple testing as shown here. I know JMeter is more powerful and configurable but working with its UI and complexity may be hard for beginners. I didn’t suspect that this post will have this much volume so I decided to make it in parts. This was just an introduction and Postman have still so much to show you. Be patient please, next part will appear here soon!

If you have any questions or problems or just want to share your experiences with Postman, feel free to do it here in comments, on Twitter or even email if you want.

Resources:

https://www.getpostman.com/

https://www.getpostman.com/docs/

https://jsonplaceholder.typicode.com

 

  • Danny Dainton

    Thank’s for sharing your experiences with Postman, i’m a huge fan of the tool. There are so many features packed in that you will has a massive amount of material for your next posts.

    It’s nice to see that you have used the jsonplaceholder API – I wrote a quick post recently about using the NPM module from those guys, to create practice APIs in about 30 seconds. https://dannydainton.com/2017/05/15/30-second-apis/

    Thanks again for sharing.

    Danny