Newman CLI Deep Dive — Running Postman Collections in CI/CD Pipelines
In this tutorial, you will learn about Newman CLI Deep Dive. We cover key concepts, practical examples, and best practices to help you master this topic.
Newman is Postman's command-line collection runner that integrates API tests into CI/CD pipelines, supporting reporters, data files, environment overrides, and programmatic execution via the Node.js API.
Code Example: Basic Newman CLI Commands
# Basic collection run
newman run threat-api-collection.json \
--environment staging.postman_environment.json
# With data file and reporters
newman run threat-api-collection.json \
--environment staging.postman_environment.json \
--data test-data.csv \
--reporters cli,junit,htmlextra \
--reporter-junit-export results/junit-report.xml \
--reporter-htmlextra-export results/html-report.html \
--delay-request 100 \
--timeout-request 10000
# Override environment variables
newman run threat-api-collection.json \
--env-var "baseUrl=https://staging-api.durga-antivirus.com" \
--env-var "timeout=15000"
# Folder-specific runs
newman run threat-api-collection.json \
--folder "Threat Creation" \
--folder "Authentication"
# Global variables and SSL
newman run threat-api-collection.json \
--global-var "runId=ci-build-1234" \
--insecure # Skip SSL verification for dev
Code Example: CI/CD Integration (GitHub Actions)
# .github/workflows/api-tests.yml
name: API Test Suite
on: [push, pull_request]
jobs:
api-tests:
runs-on: ubuntu-latest
services:
postgres:
image: postgres:15
env:
POSTGRES_PASSWORD: testpass
ports:
- 5432:5432
steps:
- uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: "20"
- name: Install Newman and reporters
run: |
npm install -g newman
npm install -g newman-reporter-htmlextra
- name: Start API server
run: |
docker compose -f docker-compose.test.yml up -d
sleep 10 # Wait for server
- name: Run API tests
run: |
newman run tests/postman/threat-api.json \
--environment tests/postman/ci.postman_environment.json \
--reporters cli,junit,htmlextra \
--reporter-junit-export results/junit.xml \
--reporter-htmlextra-export results/report.html \
--delay-request 50 \
--timeout-request 5000 || true
- name: Upload test results
uses: actions/upload-artifact@v4
with:
name: api-test-results
path: results/
- name: Publish test report
uses: dorny/test-reporter@v1
if: success() || failure()
with:
name: API Tests
path: results/junit.xml
reporter: java-junit
Code Example: Newman Programmatic API (Node.js)
const newman = require("newman");
async function runApiTests() {
return new Promise((resolve, reject) => {
newman.run(
{
collection: require("./threat-api-collection.json"),
environment: require("./staging.postman_environment.json"),
reporters: ["cli", "junit", "htmlextra"],
reporter: {
junit: { export: "./results/junit.xml" },
htmlextra: { export: "./results/report.html" }
},
iterationCount: 3,
delayRequest: 100,
timeoutRequest: 10000,
bail: true // Stop on first failure
},
function (err, summary) {
if (err) {
reject(err);
return;
}
const failures = summary.run.failures.length;
const assertions = summary.run.stats.assertions;
console.log(`Tests completed:`);
console.log(` Total: ${assertions.total}`);
console.log(` Passed: ${assertions.passed}`);
console.log(` Failed: ${failures}`);
if (failures > 0) {
summary.run.failures.forEach(f => {
console.log(` FAIL: ${f.error.test} - ${f.error.message}`);
});
}
resolve(summary);
}
);
});
}
// Run and exit with appropriate code
runApiTests()
.then(summary => {
process.exit(summary.run.failures.length > 0 ? 1 : 0);
})
.catch(err => {
console.error("Test run failed:", err);
process.exit(1);
});
Common Mistakes
1. Not Setting Correct Exit Codes
Newman exits with 0 on success and 1 on failure. CI pipelines use exit codes to determine build status. Do not use --suppress-exit-code in CI.
2. Ignoring Timeouts in CI
CI environments are slower than local machines. Set appropriate timeouts (--timeout-request 10000) and retries for flaky tests.
3. Missing Reporters for CI
CLI output alone is not useful in CI. Use JUnit (for CI dashboard integration) and HTML (for readable reports). Install reporters with npm.
4. Running Without Environment Validation
Validate environment variables before running. Missing baseUrl causes confusing failures. Use pre-request scripts to check required variables.
5. Not Isolating Test Data
Shared test environments cause data conflicts. Use unique prefixes per CI run (runId) or dedicated test accounts.
Practice Questions
- How do you install Newman and reporters?
- What is the difference between --folder and running the full collection?
- How do you pass CI-specific variables to Newman?
- What reporters are most useful for CI integration?
- How do you handle test failures in CI without stopping the full suite?
Answers:
- npm install -g newman for CLI. npm install -g newman-reporter-htmlextra for HTML reports. Install per-project to pin versions.
- --folder runs only specific folders within a collection. Full collection runs all requests. Use folders to separate test suites (auth, threats, admin).
- Use --env-var "key=value" to override specific variables. For many variables, use a CI-specific environment file with --environment.
- CLI (console output), JUnit (CI dashboard integration), HTMLExtra (readable HTML report with charts and logs), JSON (programmatic Parsing).
- Use --bail to stop on first failure for quick feedback. Without --bail, all tests run and failures are reported at the end. Both approaches are valid.
Challenge: Set up a complete Newman CI pipeline for a threat intelligence API with GitHub Actions, Docker-based test environment, JUnit and HTML reporters, data-driven tests, and artifact upload for test results.
FAQ
Mini Project
Build a complete Newman CI pipeline with: GitHub Actions workflow, Docker Compose test environment, Postman collection with 20+ tests, environment files for CI, data-driven CSV, JUnit and HTML reporters, and artifact upload with test result publishing.
What's Next
Now learn about Newman Reporters for generating detailed test reports from Newman runs.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro