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.