Table of contents
1.
Introduction
2.
Running Collection
2.1.
Sharing and Exporting Collection
2.2.
Downloading Environment
2.3.
Running from Command Line
3.
Frequently Asked Questions
3.1.
What are collections in the Postman tool?
3.2.
Can we execute a single Postman collection with multiple environments?
3.3.
Can we share a Postman collection?
4.
Conclusion
Last Updated: Mar 27, 2024

Running Collections on the Command Line with Newman in Postman

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Collections are an integral part of the postman. Running collections in Newman can be done in different ways. We can launch them from the postman application, or we can do it using the command line. We can do it once we have successfully set up Newman on our machine. This article will show how we can run collection from the command line using Newman.

Running Collections on the Command Line with Newman in Postman

Running Collection

After successfully setting up Newman on our machine, let us see if we can run the collections we created from the command line. So in order to do that we need a few things. So there are a few ways of running the collections in Newman. We can directly run our collections in Newman from the URL, which we generate, or export our collections in Newman along with the environment variables to run that . so let us go ahead and look at the collection options.

Sharing and Exporting Collection

Now we will see how we can share and export our collection. There is an option called export, and there is an option called a share. 

The Sharing Collection option

So in share, we can share it with a workspace, embed it into a page, and get a public link. 

Selecting the Share collection option and the roles by invitation.


We can use this public link to run our Collection from Newman. However, we should notice if we also require the environment variables or not. Without that, it will fail to run if our collection uses some environment variables. 

Sharing the collection with a link

If we are not using it, we can directly take this, and then we can use a command like "newman run" and run this collection URL. So it is simple: exporting the Collection in JSON format is best if there are environment variables.
So let us first create a folder, and we will use that to keep all our collection and environment variables for the postman API .so let us go and create a folder called "cnNewmanCollection". We want to store or export this in the "cnNewmanCollection" folder. 

The export option for exporting the selected collection

So the export option exports our collection in the JSON format. It will be a JSON file. So, we select that folder and keep our collection in that folder. Once we save it here, it will say our collection got exported successfully. Furthermore, we can verify that by going to that folder, which is a JSON file.

Selecting the collection version for export

Downloading Environment

We are using an example collection file, so we do not have any environment variables. Similarly, if we needed to get environment variables, we also had to download them. Without that, it will not run. So what can we do? We would download this environment and save it in the same folder. It will again be a JSON file. In the screenshot below, we can see the option of how we download and export our environment. We require these two things to run our collection from the command line using Newman if required. 

Downloading the environment

Running from Command Line

Once we have our collection and the environment, we can go back to our Newman and run this "newman run" command. And then, we need to get the collection name and give the environment if necessary. This is the kind of command we need to run in use to run our collection. So let us go ahead and do that in our command line and try Newman run.
We will go to the folder location with these particular files using the command prompt. So let us go to cd drive c drive first, and then we will go to the "cnNewmanCollection" folder. So as the subfolder, we will have all the files as JSON files. So now we will use the Newman run command, and here we will use the JSON file extension. 

We should be careful naming our files beforehand, as keeping space in the middle might not let the file run correctly, as we can see below. Our file here is named "postman_collection.json". If needed, we will use the e-tag to define the environment or to pass the environment file here. Below is the complete command on how to run our collection. We can also see that it started running. It was quick and fast because it starts from the command line, and API testing does not take much time.

Result from command line

We can also see that this will be the kind of report it will generate in the Newman. It will tell us how many iterations we ran. Whatever the requests, which failed, how many test scripts got executed, how many failed, pre-request scripts, assertions, total duration data received, Response time Etc. So everything will be shown in this tabular format. Then it will also show us all the failures and detail their expected reasons. This is how effectively we can run our collections in Newman from the command line. We can also pass the environment if needed.

Frequently Asked Questions

What are collections in the Postman tool?

In Postman, a collection is a bunch of API calls that have previously been saved in Postman and can get organised into folders. Within a collection, any number of folders can get made. Organising and documenting related requests in folders and collections in Newman assists the client in better organising and documenting their requests.

Can we execute a single Postman collection with multiple environments?

Postman monitors can only be used in one setting. To run the collections in Newman with multiple environments, the user would have to copy the individual collections, specify the environment, and duplicate the settings, such as alert emails, Etc.

Can we share a Postman collection?

We can collaborate on Postman pieces like collections, APIs, and environments by linking to them or moving them to a shared private, team, or public workspace. See Sharing a Collection Run for more information. Use the Run in Postman button to share a collection with an embed.

Conclusion

The Newman tool is in sync with the collection runner. Whatever functionality we saw in collection runner is also available in the command line. There is no difference between the features of the collection runner and the Newman tool. The only difference is here. We can run it from a command line there. We need to launch Postman and run our collection separately. So that was all about how we could run our test from the command line using the Newman tool. In this article, we read how to run collections in Newman on the command line. We also went through its features and saw ways to use them. Check out blogs and find out about React Native State and Installing and Running Newman in Postman. If you’re interested, you can find out How to Collaborate on Postman or about Managing your team and user groups in Postman. Have a look at our blogs on APIs and API Testing to explore. You can discover all about web testing too.

Recommended Reading: wc command in linux

Thank you

Explore Coding Ninjas Studio to find more exciting stuff. Happy Coding!

Live masterclass