Automate REST API testing using Postman

No matter how proficient you are, a human written code is prone to unwanted bugs and errors and hence needs to be continuously tested. With every upgrade chances of bugs increase.

We focus on unit testing but when developing RESTful APIs we also need to put our APIs to test. Generally, people use a RESTful client like POSTMAN to manually test their APIs. While POSTMAN is a good tool to test your APIs manually what most of the developers don’t know is that it can do a lot more.

Using Postman not only we can manually test APIs but we can also manage multiple environments, add variables and run multiple APIs in a chain using the runner. In short, we can use Postman to Fully automate our RESTful API testing process. Surprised? Well, even I was when I got to know about this after having used Postman for so long.

So without further ado, let’s get started and automate our API testing using Postman.

APIs

So to demonstrate this, I already have an application written in node.js which exposes CRUD APIs for the user object. If you need the source code you can download it from here.

The sample application above is a dockerized, if you are running without Docker make sure you change the MongoDB connection URI.

Let’s quickly go through the APIs we have.

POST – /users

This will help us create a user, and will return us the created user

GET – /users/{_id}

This will retrieve a user, by id

We have other APIs for updating the user and deleting the user, but we don’t need them now.

Get Postman

Environments

Before we start testing API, we need to see how environments work in Postman. This is a really handy feature, to cut out environment specific changes while testing APIs.

Whenever you are working with software you deploy your software on multiple environments, for eg: local, testing, production. That’s what the environments in Postman correspond to.

We can set environment specific variables. This is most useful for setting the host-url, coz that’s the one thing that will always change depending on the environment you are testing on.

Environments can be managed from the top right section of Postman.

Since we are working on local environment, let’s add a local environment and set our variables and see how we can use them.

Here we are creating an environment variable url and setting it to localhost:3000. Once added select the environment from the drop-down. And now we can call our APIs using the variable we just set.

Let’s create a new user. We can pass the environment variables using double curly braces, as shown in the image below.

{{url}} is set to localhost:3000

This is very useful while working with multiple environments, usually, your paths do not change only the host changes.

Testing a single API

Now let’s move ahead and write a simple test in Postman for the above API.

What we will do is check if the API returns a status 200 after creating a user.

The test tab in postman lets you write tests for that API. There are a few basic snippets available to use, but it also supports JavaScript.

Add this line to the test tab.

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

Now, hit the API.

If everything goes right you should get Test result as passed.

Chaining Tests

Testing a single API is just the beginning. The fun starts when we will test dependent APIs, meaning we will pass the output of the previous API to test the next one, aka. chaining API tests using Postman.

This kind of tests are usually required in REST APIs, the most common use-case would be to hit login to get a token and then use the token in the next secured API.

For the demo, we will create a new user, store the user’s id, and then use the user’s id to retrieve the user.

We should get that the email is matching, if it doesn’t then we know that something is messed up, so we can fix it.

Automating API testing with Postman

Testing APIs one by one is fine, but what we really need is to Automate these kinds of scenarios. Postman comes with Runner which can run APIs one after another in a collection and provide the complete result at the end of it.

In Postman collections can be managed from the left sidebar, you can also click save and select the collection you want to save to. Let’s create a collection named ciphertrick-demo, and save the above APIs to it in order. Remeber the Runner will run the APIs in the order which they are saved.

Now open up the Runner, you will find this somewhere at the top left corner. Runner opens up in a separate window.

Select our collection, environment and the iterations and hit Run.

Now Postman will call the create user API, then set the {{user_id}} and then call the get user API. Since our APIs don’t do much, it should not take more than a few seconds. Click on statistics to view the result. If everything goes well you should get all test cases as passed.

Now, whenever you make changes to your APIs all you have to do is select the right environment and hit run to test if everything is functioning as expected.

Conclusion

In this age of technology, products have a very short release cycle and hence the emphasis on testing and automation is high. This article showed us, how we can completely automate Restful API testing using Postman. Putting this into practice will save us heaps of time and eliminate unexpected Bugs.