DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Easy Oracle Database Migration with SQLcl
  • How To Develop And Deploy Micro-Frontends Using Single-Spa Framework
  • Running Axon Server in Docker
  • While Performing Dependency Selection, I Avoid the Loss Of Sleep From Node.js Libraries' Dangers

Trending

  • AI-Driven Root Cause Analysis in SRE: Enhancing Incident Resolution
  • How To Build Resilient Microservices Using Circuit Breakers and Retries: A Developer’s Guide To Surviving
  • Detection and Mitigation of Lateral Movement in Cloud Networks
  • Docker Base Images Demystified: A Practical Guide
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Deployment
  4. Using Newman to Run Postman Collections

Using Newman to Run Postman Collections

This is a brief introduction to how you can use the Newman library to make API testing automation a part of the continuous integration process.

By 
Soumyajit Basu user avatar
Soumyajit Basu
DZone Core CORE ·
Sep. 24, 21 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
15.7K Views

Join the DZone community and get the full member experience.

Join For Free

This is a brief introduction to how you can use the Newman library to make API testing automation a part of the continuous integration process. I will brief you on a few important things before on how we can set up API collections on Newman.

Newman

Newman is a command-line collection runner for Postman. It allows you to effortlessly run and test a Postman collection directly from the command line. It is built with extensibility in mind so that you can easily integrate it with your continuous integration servers and build systems.

Getting Started

To get started with the Newman setup, you need to have a node js version greater than 10. My current system has v16.9.1. 

Installation

The easiest way to install Newman is using NPM. If nodejs is installed within the system, then it is most likely that you would have npm installed as well. To verify you have npm installed, run the command npm -v. To install Newman, run the command npm install -g newman . This helps running Newman from anywhere in the system. To install locally within the current project, make it a part of the package.json. To install using homebrew in mac, run the command brew install newman.

Here, we are also going to use the Newman-HTML-reporter library to consolidate the test results within an HTML file.

Project Set-Up

Let's start implementing the API tests by creating the project directory structure. Inside the root directory of the project, I have created two directories app and report. Inside app there are two directories collections and runner. Collections will contain the collection.json exported from the postman and the runner will have the Newman runner code. Let's start by creating the dependencies of the project by running the command npm init. Once created, create a JSON object as dependencies and include the following dependencies to be installed: 

JSON
 
"dependencies": {
    "newman": "^5.3.0",
    "newman-reporter-htmlextra": "^1.22.1"
  }


Next, run npm install. This will create the package-lock.json and will install the respective modules and dependencies in the node_modules directory within the project root folder. 

Code screenshot. 

Exporting a Collection From Postman

To export a collection from the postman, click on export from collection to the collection folder in the app directory of the project.

Screenshot.

Setting Up Runner

To set up the runner, you will need to create a file called runner.js in the runner directory within the app directory. In the runner.js, we will need to give the path where the collections are present and the path where the report will be created. A point to be noted is that, if we are using global variables in postman, then we will need to keep in mind that it will not be accessible outside the scope of the postman. So, to declare global variables, we will have to declare the globalVar object where we can define the respective global variables in key-value pairs.

The runner object that I have defined looks like this:

JSON
 
const newman = require('newman');
const path = require('path');

var basepath = path.resolve("../");

var reportpath = path.resolve("../../")+"/report";


newman.run({
    // Collection URL from a public link or the Postman API can also be used
    collection: basepath + '/collection/testapiproject.postman_collection.json',
    reporters: ['htmlextra'],
    iterationCount: 1,
    globalVar:[
        {"key": "application_id", "value": process.env.apikey}
    ],
    reporter: {
        htmlextra: {
            export: reportpath + '/apireport.html',
            // template: './template.hbs'
            logs: true,
            // showOnlyFails: true,
            // noSyntaxHighlighting: true,
            // testPaging: true,
            browserTitle: "API report stats",
            title: "API report stats"
            // titleSize: 4,
            // omitHeaders: true,
            // skipHeaders: "Authorization",
            // omitRequestBodies: true,
            // omitResponseBodies: true,
            // hideRequestBody: ["Login"],
            // hideResponseBody: ["Auth Request"],
            // showEnvironmentData: true,
            // skipEnvironmentVars: ["API_KEY"],
            // showGlobalData: true,
            // skipGlobalVars: ["API_TOKEN"],
            // skipSensitiveData: true,
            // showMarkdownLinks: true,
            // showFolderDescription: true,
            // timezone: "Australia/Sydney",
            // skipFolders: "folder name with space,folderWithoutSpace",
            // skipRequests: "request name with space,requestNameWithoutSpace"
        }
    }
});


Now, we have to set up the execution, which can be utilized to run the code on the go as a part of the continuous integration process. To do this, you can define the execution within the script object in the package.json. You can define the scripts as the following:

JSON
 
"scripts": {
    "apitest": "cd app/runner && node  runner.js"
  }


Execution

To run the script, execute npm run apitest. This will run the collection using Newman and create the HTML report within the report directory. 

Reports

The reports from Newman are quite descriptive and elaborate on their own. The reports pretty much look like the following:

API Report Stats screenshot 1.


API Report Stats screenshot 2.


API Report Stats screenshot 3.

To find out more about the implementation, please look into this Github link.

Continuous Integration/Deployment Node.js Directory

Opinions expressed by DZone contributors are their own.

Related

  • Easy Oracle Database Migration with SQLcl
  • How To Develop And Deploy Micro-Frontends Using Single-Spa Framework
  • Running Axon Server in Docker
  • While Performing Dependency Selection, I Avoid the Loss Of Sleep From Node.js Libraries' Dangers

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!