0
0
Postmantesting~15 mins

Why CLI execution enables automation in Postman - Automation Benefits in Action

Choose your learning style9 modes available
Run Postman collection using CLI and verify successful execution
Preconditions (3)
Step 1: Open terminal or command prompt
Step 2: Navigate to the folder containing the Postman collection JSON file
Step 3: Run the command: newman run <collection-file-name>.json
Step 4: Observe the CLI output for test execution results
✅ Expected Result: The CLI output shows all requests executed with pass/fail status and summary of test results
Automation Requirements - Newman CLI
Assertions Needed:
Verify CLI output contains 'iterations' count
Verify CLI output shows 'failed' count as 0
Verify CLI output summary includes 'total requests' and 'total tests'
Best Practices:
Use explicit command line arguments for collection and environment files
Capture CLI output programmatically for assertions
Use exit codes to determine success or failure
Integrate CLI execution in CI/CD pipelines for automation
Automated Solution
Postman
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.

Common Mistakes - 3 Pitfalls
Not capturing CLI output for verification
Ignoring exit codes from CLI execution
Hardcoding file paths without flexibility
Bonus Challenge

Now add data-driven testing by running the same collection with three different environment files

Show Hint