This is where everything comes together. You have written tests in Postman. You have exported them. You have run them with Newman locally. Now you wire them into your CI/CD pipeline so they run automatically on every push, every pull request, every deployment. This is real automation.
# .github/workflows/api-tests.yml
name: API Tests
on:
push:
branches: [main, develop]
pull_request:
branches: [main]
jobs:
api-test:
runs-on: ubuntu-latest
steps:
- name: Checkout code
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 newman-reporter-htmlextra
- name: Run API tests
run: |
newman run collections/api-tests.postman_collection.json \
-e environments/staging.postman_environment.json \
--env-var "authToken=${{ secrets.API_AUTH_TOKEN }}" \
--env-var "apiKey=${{ secrets.API_KEY }}" \
-r cli,htmlextra,junit \
--reporter-htmlextra-export reports/api-report.html \
--reporter-junit-export reports/junit-report.xml
- name: Upload test reports
uses: actions/upload-artifact@v4
if: always() # Upload even if tests fail
with:
name: api-test-reports
path: reports/
retention-days: 30The if: always() on the upload step is critical. Without it, reports are only uploaded when tests pass. You need the report MOST when tests fail — to see what went wrong. Always use if: always() for report upload steps.
// Jenkinsfile
pipeline {
agent any
tools {
nodejs 'NodeJS-20' // Configure in Jenkins Global Tool Config
}
stages {
stage('Install') {
steps {
sh 'npm install -g newman newman-reporter-htmlextra'
}
}
stage('API Tests') {
steps {
sh '''
newman run collections/api-tests.postman_collection.json \
-e environments/staging.postman_environment.json \
--env-var "authToken=${API_AUTH_TOKEN}" \
-r cli,htmlextra,junit \
--reporter-htmlextra-export reports/api-report.html \
--reporter-junit-export reports/junit-report.xml
'''
}
}
}
post {
always {
// Publish JUnit results in Jenkins dashboard
junit 'reports/junit-report.xml'
// Archive HTML report
archiveArtifacts artifacts: 'reports/*.html', allowEmptyArchive: true
}
}
}For a cleaner CI setup, define Newman commands as npm scripts. This keeps the CI configuration simple and your test commands in one place.
{
"name": "api-tests",
"scripts": {
"test:dev": "newman run collections/api-tests.json -e environments/dev.json -r cli",
"test:staging": "newman run collections/api-tests.json -e environments/staging.json -r cli,htmlextra --reporter-htmlextra-export reports/staging-report.html",
"test:smoke": "newman run collections/api-tests.json -e environments/prod.json --folder 'Smoke Tests' -r cli",
"test:data": "newman run collections/api-tests.json -e environments/staging.json -d data/test-data.csv -r cli,htmlextra --reporter-htmlextra-export reports/data-report.html",
"test:ci": "newman run collections/api-tests.json -e environments/staging.json -r cli,junit --reporter-junit-export reports/junit.xml"
},
"devDependencies": {
"newman": "^6.0.0",
"newman-reporter-htmlextra": "^1.23.0"
}
}# Now in your CI pipeline, just run:
npm test:ci
# Or locally:
npm run test:dev
npm run test:stagingNever commit environment files with real secrets to git. Use CI/CD secrets (GitHub Secrets, Jenkins Credentials) and pass them via --env-var. If someone clones your repo, they should NOT have access to your staging API keys.
The most powerful CI/CD pattern: deploy your app, then immediately run Newman against the freshly deployed instance. If tests fail, roll back automatically.
# GitHub Actions — deploy then test
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Deploy to staging
run: ./deploy.sh staging
api-tests:
needs: deploy # Wait for deployment to finish
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: '20'
- run: npm install -g newman
- name: Run tests against fresh deployment
run: |
newman run collections/api-tests.json \
--env-var "baseUrl=https://staging.myapp.com/api" \
-r cliQ: How do you integrate Newman into a CI/CD pipeline? Walk me through the setup.
A: Step 1: Export collection and environment JSON files from Postman and commit them to your repo (without secrets). Step 2: In your CI config (GitHub Actions YAML, Jenkinsfile, etc.), add steps to install Node.js and Newman. Step 3: Run newman with the collection, environment, and --env-var flags for secrets from CI secrets store. Step 4: Use -r junit to generate JUnit reports that CI tools can parse. Step 5: Upload reports as artifacts with if:always() so they are available even when tests fail. Step 6: Newman exit code 1 automatically fails the pipeline step if any test fails. Optionally, define npm scripts in package.json to keep CI config clean.
Key Point: Newman in CI/CD: install via npm, run with --env-var for secrets from CI secrets store, use JUnit reporter for dashboards, upload reports with if: always().