Learn to Execute Postman Collections Using Newman:
In this tutorial, we will see how we can integrate or execute Postman collections through the command line using Newman which is a command-line integration tool for Postman.
Newman is a free and open-source tool. It provides powerful capabilities to run the Postman collections, leveraging super-useful capabilities of Postman like Tests, Assertions, Pre-request scripts, etc and running the collection through the command line.
=> Check Here To See A-Z Of Postman Training Tutorials Here.
What You Will Learn:
What Is Newman?
Newman is a command-line runner for Postman collections. In other words, it allows a user to run an existing Postman collection through the command line.
It expects or consumes the JSON version of the collection that can be obtained by simply exporting the collection in JSON collection format or the URL of the collection which is nothing but the same JSON that’s obtained by the collection export.
Here is a Video Tutorial:
Installing Newman
Newman is a NodeJS module and thus is dependent on the system having node installed. To check if the node is installed or not, simply check the node version on the system using the below command.
$ node -v v10.15.3
If the command returns some output as above, then it means that node is installed and we are good to go with Newman installation. If not, you can install node by referring the install instructions here
Once the node installation is successful, you can simply install Newman like any other node package using the below command.
npm install -g newman
Here ‘-g’ denotes global installation which means that Newman package will be accessible from any folder/location on the file system. For doing a local install, you can remove the ‘-g’ flag from the above, where the Newman package will be accessible only from the installed location or folder.
To validate the successful installation of Newman, you can simply check its version using the below command.
newman -v 4.5.1
Running Collections Using Newman
To run collections using Newman, you should have any one of the 2.
- The collection in JSON format.
- URL of the hosted collection.
The command used to run the Postman collection using Newman is:
newman run {{collectionJsonPath}} OR newman run {{collectionUrl}}
Let’s try running a sample collection using Postman.
- Go to the Postman application, use any existing Postman collection and export it to JSON form. (We will create a new collection with 3 requests i.e. Register User, Login User and Get User using API endpoints form here).
- Now export the collection to JSON format.
- Once the collection JSON is obtained, open a command prompt and run the collection using Newman run command (suppose the collection was exported with name – Postman_Newman_IntegrationCollection.json) and the user has browsed to collection export directory, then the collection can be executed simply by using the below command.
newman run Postman_Newman_IntegrationCollection.json
Once the above command is run, the following output is produced.
Newman Integration With Environment Variables
Now let’s see more advanced usages of Newman. For a collection that does not rely on any environment variables, the collection could be simply executed using the Newman run command. But for collections, using the environment variables, we need to provide the environment variable JSON as well along with the collection JSON.
Example: We will use the same collection and use an environment variable for GET User endpoint i.e. for request with endpoint.
We will use userId from the environment variable collection. So the resultant request will become https://reqres.in/api/users/{{userId}}
Now once the environment file is created, export it as a JSON, using the below steps.
- Create an environment, with one variable named userId and the value should be set to ‘4’.
- Now, click on the Settings button, to open the ‘Manage Environments’ window.
- Click the ‘Download’ icon against the created environment to download the environment as a JSON file (save the file with name as ‘testEnv.json).
Let’s export the collection again with the changed request, and try running the same collection along with environment file with the command as below:
newman run Postman_Newman_IntegrationCollection.json -e testEnv.json
Once the above command is executed, the output remains the same as the direct collection run, with the only change being the ‘GET User’ request now fetches the value from the environment JSON file.
Assertion Results Using Newman
As the Postman requests can contain assertions as well, we will now walkthrough, how the assertion results are displayed when the Postman collections are executed through a Newman.
The Newman collection runner is in a complete parity with the Postman Collection runner and Request executor. For requests having assertions, the assertions get evaluated as and when the request execution completes and the summary of the assertion execution is displayed in the test summary at the end of the test run.
For the above collection, we did add a collection level assertion to check the status code of the response to be 200 i.e. for every request that’s a part of the collection there should be this assertion associated.
So if this collection had 3 requests, it means that there should be a total of 3 assertions that should have got executed.
Please refer to the below screenshot for highlighted Execution results and Assertion results.
Report Generation Using Newman
So far we know that Newman can run Postman collections through the command line and generate some test logs and test execution summary. But what about the formatted reports that could be shared or published on some server?
Well, Newman has support for this as well. There are some custom node modules available for generating Newman test execution reports. We will walk through an example using a newman-html-reporter.
This reporter is again a node module and has to be separately installed using the below command.
npm install -g newman-reporter-html
Once the above module is installed, this can be used along with the Newman run command.
newman run Postman_Newman_IntegrationCollection.json -e testEnv.json -r html
The ‘-r’ flag, indicates the reporter to be used with the Newman collection run.
With this option, it makes use of the Newman-reporter-html module or packages and creates an HTML based report for the test execution.
Refer to one such screenshot of the HTML report shown below:
Integration With CI Tools
With Newman having the capabilities to run as a command line, it reduces a lot of dependency on any pre-requisites in the form of a console or an application i.e. the only dependency that Newman has is a node (which is generally available as an execution environment in all CI machines like Jenkins slaves or Travis nodes).
This enables Postman collections to be executed as a part of the build pipelines itself through the Newman command line.
With Newman having capabilities of pushing results in Html format as well, this is really helpful and handy and during the pipeline execution itself, the HTML results could be pushed to a server or can be sent through an email to the desired users, etc. The capabilities are endless as the dependencies are minimal.
For an end to end detailed execution example of integration with Jenkins, please refer to this example from Postman’s official blog here.
More Options With Newman
Whatever we have covered is a subset of the functionality that’s provided by Newman. For complete details of commands and switches that Newman supports, simply open command-line help for Newman using the below command.
newman run -h
Here is the output of the above command, with details about all the switches and their meanings.
There are other information resources as well for Newman API reference. Please refer here for the official documentation.
Conclusion
In this tutorial, we walked through the command line integration of Postman called Newman, which allows running Postman collections through a command-line interface.
It’s simply a node package and any command-line that has node installed along with Newman should be able to run the Postman collection and generate good looking reports of the collection execution.
Command-line integration is also of great help for integration of Postman based collection tests with CI tools like Jenkins, Travis, etc, as running through command line does not have any dependencies on OS or application and it just simply needs node environment to run the collection.
=> Visit Here For The Exclusive Postman Training Tutorial Series.