Batman and Robin of API Testing

We worship our heroes but tend to ignore the sidekick of the hero. We love Batman – but what about Robin?

Let me introduce you to the Batman and Robin of the API Testing! We all know who the Batman of API testing is! Yes, Postman. We will not talk about Postman in this post though (maybe some other time). But let me talk about the second fiddle to Postman i.e. Newman.

To be fair – I didn’t know the existence of Newman until 2 weeks back. My colleague introduced it to me I instantly loved it because of its simplicity.

What is Newman (aka Robin in this post)?

Newman is a command line interface (CLI) for running Postman collections and visualizing the results. Since it’s a command line tool, it can be integrated very naturally with your CI pipelines.

Newman takes Postman collection, and environment JSON files as arguments and can not only run the API tests but can generate beautiful reports as well.

Newman is Open source and all you need is its NPM package.

How do I get it installed on my Windows Machine?

Step 1: Get Node.js

Download and install Node.js on your machine unless you already have it. Newman runs on Node, so it’s a hard dependency. Make sure you are downloading Node.js version 4 or above. You can download Node.js using these steps.

Step 2: Get Newman

Install Newman from NPM from your command line.

C:\Users\girish.r.acharya\Downloads>npm install -g newman

Step 3: Use Newman against the Postman collection you already have

Export the Postman collection as a JSON file at an appropriate path and execute the command as shown below. (In this example, my Postman collection JSON file is at the same path where I am running the Newman) See the magic! It shows a nice summary/report as a response.

Note: Notice how the collection file name is passed as a command line argument to the command using a switch called run.

C:\Users\girish.r.acharya\Downloads>newman run "Newman Demo.postman_collection.json"

Step 4: Get the report as a nicely formatted HTML.

The command below will help output the test results as a nicely formatted HTML file.

C:\Users\girish.r.acharya\Downloads>newman run "Newman Demo.postman_collection.json" -r htmlextra

Note: Notice how an extra switch htmlextra is passed to the command. This will ensure that the output is written to an HTML file instead of showing on the command line.

Note: the HTML file is created in a folder called newman at the same path where your Postman collection JSON file lives.

Step 5:Exploring more arguments/switches

Newman supports a lot of optional arguments. Here are a few that I used and loved:

  1. –environment or -e: Using this switch, you can pass a Postman environment JSON file.
  2. –delay-request: Using this switch, you can introduce some delay between the request execution in milliseconds.
  3. –bail: This flag can be used to tell Newman to stop on a test case error with a status code of 1, which can then be picked up by a CI pipeline.
  4. –suppress-exit-code or -x: By default, Newman exits with a status code of 0 if everything is successful which is perfect from the CI pipeline point of view. But one can use -x or –suppress-exit-code switch to override the default exit code.
C:\Users\girish.r.acharya\Downloads>newman run "Newman Demo.postman_collection.json" -e "Newman Demo.environment.json" -r htmlextra

Obviously, Newman supports more arguments and those can be found here. Feel free to explore.

Don’t forget the ultimate sidecar to your beloved Postman – Newman! Robin deserves your love and care as much as Batman.

Leave a comment