Run Postman collection using CLI and verify successful execution
Preconditions (3)
✅ Expected Result: The CLI output shows all requests executed with pass/fail status and summary of test results
import subprocess import json # Define the collection file path collection_file = 'sample_collection.json' # Run Newman CLI command result = subprocess.run(['newman', 'run', collection_file], capture_output=True, text=True) # Print CLI output print(result.stdout) # Basic assertions on CLI output assert 'iterations' in result.stdout, 'Iterations count missing in output' assert 'failed: 0' in result.stdout.lower(), 'There are failed tests' assert 'total requests' in result.stdout.lower(), 'Total requests info missing' assert result.returncode == 0, 'Newman CLI exited with error'
This script uses Python's subprocess module to run the Newman CLI command that executes the Postman collection.
We capture the CLI output as text to verify key information like iterations, failed tests, and total requests.
Assertions check that the output contains expected summary details and that the exit code is zero, indicating success.
This approach shows how CLI execution enables automation by allowing scripts to run tests and verify results without manual intervention.
Now add data-driven testing by running the same collection with three different environment files
newman run mycollection.json --reporters cli,json
newman run
Pass the test and this pattern moves unknown