0
0
Postmantesting~15 mins

Newman in CI/CD pipelines in Postman - Build an Automation Script

Choose your learning style9 modes available
Run Postman collection using Newman in CI/CD pipeline
Preconditions (3)
Step 1: Configure the CI/CD pipeline to include a step to run Newman
Step 2: Use the command: newman run <collection-file.json> -e <environment-file.json>
Step 3: Execute the pipeline
Step 4: Observe the output logs for test run results
✅ Expected Result: Newman runs the Postman collection successfully, all tests pass, and the pipeline logs show a summary of the test results
Automation Requirements - Newman CLI
Assertions Needed:
Verify that Newman command exits with status code 0 indicating success
Verify that the test summary shows all tests passed
Verify that the pipeline logs contain the expected test run output
Best Practices:
Use explicit exit code checks to determine pass/fail
Use environment variables for collection and environment file paths
Integrate Newman command in a script step in the CI/CD pipeline configuration
Capture and parse Newman JSON report for detailed assertions if needed
Automated Solution
Postman
import subprocess
import json

# Paths to collection and environment files
collection_file = 'postman_collection.json'
environment_file = 'postman_environment.json'

# Run Newman command with JSON reporter
command = [
    'newman', 'run', collection_file,
    '-e', environment_file,
    '--reporters', 'cli,json',
    '--reporter-json-export', 'newman_report.json'
]

result = subprocess.run(command, capture_output=True, text=True)

# Check exit code
assert result.returncode == 0, f"Newman run failed with exit code {result.returncode}"

# Load JSON report
with open('newman_report.json') as f:
    report = json.load(f)

# Check all tests passed
assert report['run']['stats']['failed'] == 0, "Some tests failed in Newman run"

print("Newman run successful: all tests passed.")

This script runs the Newman CLI command using Python's subprocess module. It specifies the collection and environment files, and requests both CLI and JSON reports.

After running, it checks the exit code to confirm the run was successful.

Then it loads the JSON report file and asserts that zero tests failed.

This ensures the CI/CD pipeline can detect failures and pass/fail accordingly.

Common Mistakes - 3 Pitfalls
Not checking the exit code of the Newman command
Hardcoding file paths inside the script
Not using explicit reporters like JSON for parsing results
Bonus Challenge

Now add data-driven testing by running the same Postman collection with three different environment files in the CI/CD pipeline.

Show Hint