API Testing Using Postman and Newman

Image for post
Image for post

In the last few years, we have an exponential increase in the development and use of APIs. We are in the era of API-first companies like Stripe, Twilio, Mailgun etc. where the entire product or service is exposed via REST APIs. Web applications also today are powered by REST-based Web Services. APIs today encapsulate critical business logic with high SLAs. Hence it is important to test APIs as part of the continuous integration process to reduce errors, improve predictability and catch nasty bugs.

In the context of API development, Postman is great REST client to test APIs. Although Postman is not just a REST Client, it contains a full-featured testing sandbox that lets you write and execute Javascript based tests for your API.

Postman comes with a nifty CLI tool — Newman. Newman is the Postman’s Collection Runner engine that sends API requests, receives the response and then runs your tests against the response. Newman lets developments easily integrate Postman into continuous integration systems like Jenkins. Some of the important features of Postman & Newman include:-

  1. Ability to test any API and see the response instantly.
  2. Ability to create test suites or collections using a collection of API endpoints.
  3. Ability to collaborate with team members on these collections.
  4. Ability to easily export/import collections as JSON files.

We are going to look at all these features, some are intuitive and some not so much unless you’ve been using Postman for a while.

Setting up Your Postman

Image for post
Image for post

Later, can then look it up in your installed apps and open it. You can choose to Sign Up & create an account if you want, this is important especially for saving your API collections and accessing them anytime on any machine. However, for this article, we can skip this. There’s a button for that towards the bottom when you first launch the app.

Image for post
Image for post

Postman Collections

There’s a Collections tab on the top left of Postman, with an example Postman Echo collection. You can open and go through it.

Image for post
Image for post

Just like in the above screenshot, select an API request and click on the Tests. Check the first line:

The above line is a simple test to check if the response code for the API is 200. This is the pattern for writing Assertions/Tests in Postman (using JavaScript), and this is actually how you are going to write the tests for API’s need to be tested. You can open the other API requests in the POSTMAN Echo collection to get a sense of how requests are made.

Adding a Collection

Image for post
Image for post

You will be prompted to give details about the collection, I’ve added a name Github API and given it a description.

Image for post
Image for post

Clicking on Create should add the collection to the left pane, above, or below the example “POSTMAN Echo” collection.

If you need a hierarchy for maintaining relevance between multiple API’s inside a collection, APIs can further be added to a folder inside a collection. Folders are a great way of separating different parts of your API workflow. You can be added folders through the “3 dot” button beside Collection Name:

Image for post
Image for post

Eg.: name the folder “Get Calls” and give a description once again.

Image for post
Image for post

Now that we have the folder, the next task is to add an API call that is related to the TEST_API_COLLECTION to that folder. That API call is to https://api.github.com/.

If you still have one of the TEST_API_COLLECTION collections open, you can close it the same way you close tabs in a browser, or just click on the plus button to add a new tab on the right pane where we make requests.

Type in or paste in https://api.github.com/ and press Send to see the response.

Image for post
Image for post

Once you get the response, you can click on the arrow next to the Save button on the far right, and select Save As, a pop up will be displayed asking where to save the API call.

Image for post
Image for post

Give a name, it can be the request URL or a name like “GET Github Basic”, and a description, then choose the collection and folder, in this case, TEST_API_COLLECTION> GET CALLS, then click on Save. The API call will be added to the Github Root API folder on the left pane.

Image for post
Image for post

Whenever you click on this request from the collection, it will open in the center pane.

Write the Tests

Image for post
Image for post

You will notice on the right pane, we have some snippets which Postman creates when you click so that you don’t have to write a lot of code. Let’s add Response Body: JSON value check. Clicking on it produces the following snippet.

Image for post
Image for post

From these two lines, it is apparent that Postman stores the response in a global object called responseBody, and we can use this to access response and assert values in tests as required.

Postman also has another global variable object called tests, which is an object you can use to name your tests, and equate it to a boolean expression. If the boolean expression returns true, then the test passes.

If you click on Send to make the request, you will see one of the tests failing.

Image for post
Image for post

Let's create a test that relevant to our usecase.

Image for post
Image for post

Clicking on 'Send', you'll see the test passing.

Image for post
Image for post

Let’s modify the test further to test some of the properties we want to check:

Image for post
Image for post

Ideally, the things to be tested in an API Response Body should be:

  • Response Code ( Assert Correct Response Code for any request)
  • Response Time ( to check api responds in an acceptable time range / is not delayed)
  • Response Body is not empty / null

Newman CLI

All you need to do is export your collection and the environment variables, then use Newman to run the tests from your terminal.

NOTE: Make sure you’ve clicked on ‘Save’ to save your collection first before exporting.

Using Newman

Image for post
Image for post

Select version 2, and click on “Export”

Image for post
Image for post

Save the JSON file in a location you can access with your terminal. I created a local directory/folder called “postman” and saved it there.

Install Newman CLI globally, then navigate to the where you saved the collection.

Using Newman is quite straight-forward, and the documentation is extensive. You can even require it as a Node.js module and run the tests there. However, we will use the CLI.

Once you are in the directory, run newman run <collection_name.json>, replacing the collection_name with the name you used to save the collection.

Image for post
Image for post

NEWMAN CLI Options

Let's try out of some of the options.

Iterations

To provide a different set of data, i.e. variables for each iteration, you can use the -d to specify a JSON or CSV file. For example, a data file such as the one shown below will run 2 iterations, with each iteration using a set of variables.

Alternately, the CSV file for the above set of variables would look like:

Environment Variables

To provide a different execution environment, you can use the -e to specify a JSON or CSV file. For example, a environment file such as the one shown below will provide the environment variables globally to all tests during execution.

Bail FLAG

Conclusion

*******************************************************************

This post was originally published on Velotio Blog.

Velotio Technologies is an outsourced software product development partner for technology startups and enterprises. We specialize in enterprise B2B and SaaS product development with a focus on artificial intelligence and machine learning, DevOps, and test engineering.

Interested in learning more about us? We would love to connect with you on our Website, LinkedIn or Twitter.

*******************************************************************

Velotio Perspectives

Thoughts and ideas on startups, enterprise software &…

Velotio Technologies

Written by

Velotio Technologies is an outsourced software and product development partner for technology startups & enterprises. #Cloud #DevOps #ML #UI #DataEngineering

Velotio Perspectives

Thoughts and ideas on startups, enterprise software & technology by the Velotio team. Learn more at www.velotio.com.

Velotio Technologies

Written by

Velotio Technologies is an outsourced software and product development partner for technology startups & enterprises. #Cloud #DevOps #ML #UI #DataEngineering

Velotio Perspectives

Thoughts and ideas on startups, enterprise software & technology by the Velotio team. Learn more at www.velotio.com.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store