Tips to simplify API testing with Postman

03May,18 Post Image

A few months ago I started developing what started out as a pretty straightforward API. At the time I thought it was going to be only two or three different API calls, so i didn’t give much thought to testing. But scope creep got the best of me and my innocent text file with test curl commands quickly became a monster. A couple of weeks later the two or three API calls became 10-plus and my little text file was no longer a viable solution for testing.

Enter Postman. I’ll admit I had heard about it before but I’d never given it a go because I kept thinking I would lose more time setting up things than if I just kept testing manually. But testing this project became such a pain that I decided something needed to change. I took the time to automate the testing part with Postman and after that, the pain of testing went away. So now allow me to share a few tips I learned from the experience.

Multiple environments, multiple friends

Don’t be afraid to use multiple environments, they are super easy to set up and will save you a lot of time if you use them.

  • Don’t input the endpoint URL in all your API calls, just set the value in an environment variable and use that variable everywhere.
  • Do you need an authorization token to call your API? Set an environment variable for that too. You can use the double braces syntax {{ }} to call your environment variables from any part of your request, the URL, Headers or Body.
  • If you see a value repeating itself a bunch of times you know what to do. Also, don’t forget you can peek at the values by clicking on the eye next to the environment drop-down list.

postman1

postman2

Keep it clean

This is a simple one but it’s really easy to miss. Use the collections sidebar to your advantage. At first a project might seem small but you never know when things might grow unexpectedly. Also, chances are you’ll have multiple projects at the same time so having a different folder for each one will keep things organized and easy to maintain.

postman3

Let the Response flow

There will come a moment when you’ll need to use one of the values of the Response of one request in the Body of another request. You might think, “I’ll just copy and paste it, it’s no big deal!” Well, try doing it 20 times in a row and it will become a big deal. Think automation and use Postman’s abilities to your advantage. There is a section called “Tests” in Postman where you can write JavaScript to do all sorts of things.

For example, with the following code snippets you can parse the globally available variable “responseBody” and store one of its values for later use as an environment variable:

var jsonData = JSON.parse(responseBody);
postman.setEnvironmentVariable("idToken", jsonData.body.message.idToken.jwtToken);

Now that you have stored the value in the environment just use the double braces syntax {{ }} to use it somewhere else.

Check all Responses

We always need to check that the Responses from an API call contain all the values they should contain. The thing is that this task can get too complicated to do by hand really easily. Responses can be too long to check with your own eyes so just write some simple tests to check that everything is where it is supposed to be. In the “Tests” section of the Request use the globally available “tests” array to do some simple checks and confirm all your calls are returning what they are supposed to return.

var jsonData = JSON.parse(responseBody);
tests["Status code is 200"] = responseCode.code === 200;
tests["Response contains instances"] = jsonData.body.message.instances !== null

You can write any number of tests like that and the results will be shown in the Postman UI after each call so that you can keep track when something breaks.

postman4

These are only a few of the simple tricks that you can use to automate testing tasks in Postman. There are a lot more — like running a complete collection of requests in sequence and collecting logs and test results along the way.

The next time you find yourself overwhelmed by API testing give Postman a shot and automate the whole thing. Good luck!

Subscribe to Our Newsletter

Join our community of DevOps enthusiast - Get free tips, advice, and insights from our industry leading team of AWS experts.