Meet Newman: a command-line companion for Postman

Tahera Firdose
5 min readJul 11, 2021

--

What is Newman?

Newman is a powerful command-line collection runner for Postman. It allows you to run and test a Postman collection from the command line with ease. It’s designed to be extensible, so you can easily connect it to your continuous integration servers and build systems. Newman maintains feature parity with Postman and allows you to run collections the way they are executed inside the collection runner in Postman. This makes Postman and Newman a unique combination.

How to Install Newman?

Installing NodeJS is pretty straightforward, visit their download page here, download your platform version to install. Kindly make sure it's version 10 or higher.

After installing, head to your Terminal and enter the command below to check if everything is good to go.

After installing Nodejs and npm, you can run the below command.

npm install -g newman

Create a Collection of API Calls

What is a Collection?

A collection is a container for all of your API requests. The requests can be organized into folders so that it can be run independently. To create a collection click on New -> Collection, give a name and add a description (Optional) to your collection and then click on create.

I have created a collection with the name Newman Collection and added Create User, List User, and Get Single User request as shown in the below image.

Add Environment Variables:

Once you have a collection of requests built up you may want to run these against different environments. Add some variables by clicking on Environments on the left-hand side.

Click Add to create a new variable:

After you have added all of the environments, you may access them by substituting the base url with the variable (URL) you just made:

Now that we have added all our requests in Newman collection and created the environment variables. It’s time for us to try to execute everything in our collection from the command line using Newman.

There are two ways to run a collection through Newman

1. Running Newman using the collection URL

2. Running Newman using the JSON file

Now let us run the collection with the above methods:

Running the collection using Newman through the collection URL

  1. Hover your mouse over the three dots next to the collection name and click on share collection.

2. Click on Get Public link.

3. Copy the link below.

4. If your collection uses environment variables, then make sure to export the environment JSON file.

Download the environment JSON file by navigating to environments, click on the 3 dots and click export.

5. Save the JSON file to your computer.

6. Open your shell (command prompt for windows and terminal for mac) where the environment variable file is stored on your system and run the following:

newman run <above link copied from step3> — environment “name of the environment file”

7. Press Enter

If you see the following screen, your collection was successful.

We can see from the above image the test details we see are similar to the one displayed in postman. We can see the number of iterations, requests, test scripts, assertions, the status of each request (response time, size, and status code). The failed requests are tracked under failed section and displayed in red color.

8. Let us now update the test scripts with the proper assertions and rerun the test. But each time we make changes to the collection, we need to get the updated link as shown below.

Click on the Update link and copy the new link.

9. Rerun the tests with the new link copied above.

Running the collection using Newman through JSON File

  1. Hover your mouse over the three dots next to the collection name and click on export.

2. Click on Export again on the Export collection.

3. Save the JSON file to your computer.

4. If your collection is using environment variables, then make sure to export the environment JSON file.

5. Download the environment JSON file by navigating to environments, click on the 3 dots and click export.

6. Save the environment JSON file to your computer.

7. After you have saved the JSON file, navigate to the directory through the shell where your JSON files are stored.

8. After changing the directory, run the following command.

newman run “name of the collection file” — environment “name of the environment file”

9. Press enter and you will see the expected results of your collection.

Happy Testing!!

--

--

Tahera Firdose
Tahera Firdose

Written by Tahera Firdose

Datascience - Knowledge grows exponentially when it is shared

No responses yet