fbpx Postman: Command Line Integration of Newman CLI with Slack App

Postman: Command Line Integration of Newman CLI with Slack App


Software engineers perform their tests locally, and most of the team members may not know if they have even carried out the tests or not. For now, let’s forget the test results. We want to convey information to our team members in an organized way that will help us make better decisions. 

Sounds interesting, eh? Today, we will discuss how to send test reports directly to the Slack app so that all team members can see the results. We will walk you through all the necessary steps to achieve this. Let’s take a look at the process, why such packages are necessary, and how we will accomplish them. 

What is Newman?

Newman is a command-line collection runner for Postman. Newman collection runner sends API requests, receives the response from the server and runs the test scripts you have developed for your application. All this can happen from the command line with just one command. Looking for Continuous Integration, you can also integrate it with CI tools like Jenkins, Bamboo, Circle CI. 

For more details, you can check the documentation of the Postman. 

Why do we need Newman?

You may have your collection and environment files in the Postman application itself. But most of the time, it’s tedious to run the collection and specify its environment one by one. 

That is where Newman comes in. You can run the collection using a single command from the command line using Newman. To do that, you need to fulfill the prerequisite mentioned in this article, so stay with us.  

How do we install the Newman in your machine?

Prerequisites

First, you need to install node on your machine. 

Link: https://nodejs.org/en/

After installing Nodejs on your machine, open the command tool and run the following command to check the version of npm. 

npm -v 

Now, let’s move on to the next step and install the Newman (Command-line collection runner for Postman). Run the following command, and it will install the Newman globally in your local machine. If you do not want to install Newman globally, remove ‘-g.’

npm install -g newman 

It’s time to verify whether the Newman is installed correctly or not. Run this command to check the version. 

newman -v

Note: Newman setup is now completed. One thing to keep in mind throughout this process is that we will be installing all the packages globally.

Running YOUR Collections and Environment Locally

You can easily run the collection you have in your local machine with a simple command. Go to the file path of the collection that you have in your local machine. 

my-collection.postman_collection.json

If you do not have a collection file in your local machine, you can export a collection to share as a file. We can also share the collection file using the URL.

You may have variables that are needed for the collection that you have exported earlier on. To export an environment file, simply export an environment file from the postman tool. We can also share the environment file using URL. Let’s run a command to run the collection with its environment variables.

newman run my-collection.postman_collection.json -e my-environment.postman_environment.json

So, what happens if you do not have the environment file needed for your collection file? You can simply move on to the next step!

Now, we have successfully run our collection with its environment variable using Newman. YAYY!! Pat yourself on your back. 

For more information, you can visit this link.

Create a Slack App

It’s now time to create a slack app where we can send the test reports. 

First, we need to create a slack app. 

Navigate to the URL below. https://api.slack.com/

Click on the Create an app. 

Click on the “Create New App.” 

A kind of dialog appears on the screen. Select the first option. 

Let’s provide the slack name as “newman-runner” and its workspace name as “My Workspace.”

Click on “Create App” to create an app. Now, we have created an app called “newman-runner” and specified its workspace. Since we need to send reports to the slack app, we need to activate the webhook.

Click on the “Incoming Webhooks” from the sidebar. Click on the button to activate the incoming webhooks. 

Scroll down and click on the “Add New Webhook to Workspace.”, 

You can select the channel. You have now successfully created a webhook URL. Copy the URL and paste it where you can access it. We need this webhook URL at a later part. 

You will see a message in which the app is integrated. Something similar like this. We have created a slack app and webhook is also integrated. 

Report Generation

We need to send test reports to the slack channel that we have integrated into our previous step. Now, we need to understand what kind of report we need to send. 

Let’s install the package on your local machine. Again, ‘-g’ means the package is installed globally. 

npm install -g newman-reporter-slack

You can find out different reporter options here

Combining everything that we know

  1. Run the collection using the command-line tool of Newman
  2. Slack Webhook URL
  3. Report Generation

Finally, let’s combine what we have understood so far and RUN the following command.

newman run my-collection.postman_collection.json -e my-environment.postman_environment.json –suppress-exit-code -r slackmsg –reporter-slackmsg-webhookurl https://hooks.slack.com/services/aaa/bbb/ccccc

Running Newman is a great start in keeping the team informed about the status of the application you are working on. We have shown the test results through the Slack app that we integrated earlier on. This is going to bring everyone’s understanding on the same page to some extent. On the other side, it provides actionable data to the different stakeholders and helps to maintain quality across the organization.

Happy Testing!

Saroj Karki

Saroj Karki is a senior Software Engineer- Quality Assurance at Leapfrog.

More in Blogs

Secrets to Effectively Managing Testing Process GeneralInsights

Secrets to Effectively Managing Testing Process

“Good food depends almost entirely on the right  ingredients”  Testing processes and activities that add value in delivering quality products

Read more
Social Media: Impact on Company & Business Image General

Social Media: Impact on Company & Business Image

Social media can play a vital role in shaping your brand. Whether big or small business, it is important to

Read more