Newman CI/CD Integration Guide
Introduction to Newman
Newman is a command-line collection runner for Postman that allows you to run and test a Postman collection directly from the command line but primarily with CI/CD setup. It’s particularly useful for:
-
Automating API testing in build processes
-
Running collections on servers without a GUI
-
Scheduling routine API checks
-
Generating test reports for documentation
Required Project Structure
A typical Newman project structure includes:
/project-root
/newman
newman.js # Newman configuration script
collection.json # Exported Postman collection
environment.json # Exported Postman environment
data.json # Test data for parameterized runs
Newman Configuration
newman.js Configuration File
A newman.js file allows you to programmatically run collections with custom settings:
const newman = require('newman');
const path = require('path');
const collection = path.resolve(__dirname, 'collection.json');
const iterationData = path.resolve(__dirname, 'data.json');
const environment = path.resolve(__dirname, 'environment.json');
newman.run({
collection: collection,
iterationData: iterationData,
environment: environment,
reporters: 'htmlextra',
reporter: {
htmlextra: {
export: './reports/newman-report.html',
omitHeaders: true,
title: 'My Awesome Report Title'
}
},
insecure: true, // Skip SSL certificate verification
suppressExitCode: true // Won't exit due to test failures
}, function (err, summary) {
if (err) {
throw err;
}
console.log('Newman collection run complete!');
});
|
Important Configuration Options:
|
Exporting Collections and Environments
To export collections and environments from Postman:
-
For collections:
-
Click the "…" next to the collection
-
Select "Export"
-
Choose "Collection v2.1"
-
Select "Export"
-
Use Finder and place into project newman dir as collection.json
-
-
For environments:
-
Go to the Environments tab
-
Click the "…" next to the environment
-
Select "Export"
-
Use Finder and place into project newman dir as environment.json
-
Creating Data Files
Data files allow you to run the same collection multiple times with different sets of data, aka Iterations:
[
{
"icn": "1013981395V971305",
"dfn983": "7227000",
"dfn984": "552166195",
"edipi": "2116630888"
},
{
"icn": "1012845331V153043",
"dfn983": "7216691",
"dfn984": "552161050",
"edipi": "1259897978"
},
{
"icn": "1013120787V412913",
"dfn983": "7219320",
"dfn984": "552165566",
"edipi": "1921024322"
}
]
Use these values in your collection with {{icn}}, {{dfn983}}, etc.
|
Benefits of Using Data Files:
|
Setting Up Newman locally
| Not necessary unless you want to run against local deployment. Otherwise, testing collection setup in staging is preferred and more efficient. |
Installation
Newman requires Node.js to be installed on your system. Install Newman globally using npm:
npm install -g newman newman-reporter-htmlextra
Basic Usage
The basic command to run a collection with Newman from project root:
node newman/newman.js
This command will:
-
Execute all requests in the collection against a running service
-
Apply environment variables from environment.json
-
Iterate through test data from data.json (if configured)
-
Generate an HTML report in the reports directory
|
Add |
CI/CD Integration
Post-Deployment Testing
For automated testing after deployment:
* Jenkins shared library handles the post-deploy execution of Newman and the Slack reporter
* All that is needed is a simple update to jenkins.yaml:
staging:
runNewman: true
|
What Happens When Newman Runs in Jenkins:
|
Resources
Official Documentation
-
Newman GitHub: https://github.com/postmanlabs/newman
-
Newman API: https://github.com/postmanlabs/newman#api-reference
-
Newman Reporter HTMLExtra: https://github.com/DannyDainton/newman-reporter-htmlextra