Running collections via CLI in Postman - Build an Automation Script
import subprocess import json # Path to the Postman collection JSON file collection_path = 'sample_collection.json' # Run Newman CLI with JSON reporter for structured output command = ['newman', 'run', collection_path, '--reporters', 'json'] try: result = subprocess.run(command, capture_output=True, text=True, check=True) # Parse the JSON output from Newman output_json = json.loads(result.stdout) # Extract run summary run_summary = output_json['run']['stats'] # Assertions assert run_summary['iterations']['total'] == 1, f"Expected 1 iteration, got {run_summary['iterations']['total']}" assert run_summary['requests']['total'] >= 1, "Expected at least 1 request" assert run_summary['tests']['total'] >= 1, "Expected at least 1 test" assert run_summary['tests']['failed'] == 0, f"Expected 0 failed tests, got {run_summary['tests']['failed']}" print('Test run passed successfully.') except subprocess.CalledProcessError as e: print(f'Newman CLI execution failed: {e}') except json.JSONDecodeError: print('Failed to parse Newman JSON output.') except AssertionError as ae: print(f'Assertion failed: {ae}')
This script automates running a Postman collection using Newman CLI.
We use Python's subprocess module to run the command newman run sample_collection.json --reporters json. The --reporters json option outputs the results in JSON format, which is easier to parse and assert.
We capture the CLI output and parse it as JSON. Then, we check the run summary statistics:
- There should be exactly 1 iteration.
- At least 1 request should have run.
- At least 1 test should have run.
- No tests should have failed.
If all assertions pass, the test run is successful. If any assertion fails, or if the CLI command fails, we print an error message.
This approach follows best practices by using structured output, explicit command arguments, and proper error handling.
Now add data-driven testing by running the same collection with three different environment files.
Practice
Solution
Step 1: Identify the official CLI tool for Postman collections
Newman is the official command line tool designed to run Postman collections.Step 2: Differentiate from other tools
npm is a package manager, curl is a general HTTP client, and postman-cli is not an official tool.Final Answer:
newman -> Option BQuick Check:
Postman CLI tool = newman [OK]
- Confusing npm with the CLI runner
- Using curl as a Postman collection runner
- Assuming postman-cli is official
test_collection.json using Newman?Solution
Step 1: Recall the Newman command to run collections
The correct command isnewman run <collection-file>.Step 2: Verify the options
Options like execute, start, or launch are not valid Newman commands.Final Answer:
newman run test_collection.json -> Option CQuick Check:
Command syntax = newman run [OK]
- Using 'execute' instead of 'run'
- Trying 'start' or 'launch' commands
- Omitting the 'run' keyword
newman run my_collection.json -e my_environment.json --delay-request 1000, what effect does the --delay-request 1000 option have?Solution
Step 1: Understand the --delay-request option
This option adds a delay between each request in the collection, measured in milliseconds.Step 2: Interpret the value 1000
1000 milliseconds equals 1 second delay between requests.Final Answer:
Delays each request by 1000 milliseconds between executions -> Option DQuick Check:
--delay-request = wait time between requests [OK]
- Confusing delay with timeout
- Thinking it repeats the collection
- Assuming it limits total requests
newman run collection.json -e env.json --reporters cli,html but get an error saying Unknown reporter: html. What is the most likely cause?Solution
Step 1: Analyze the error message
The error states 'Unknown reporter: html', meaning Newman does not recognize the 'html' reporter.Step 2: Understand reporter support
Newman supports some reporters by default; others require installation or are unsupported.Final Answer:
The HTML reporter is not installed or supported by Newman -> Option AQuick Check:
Unknown reporter means unsupported or missing plugin [OK]
- Blaming environment or collection files
- Assuming CLI reporter conflicts
- Ignoring reporter installation requirements
data.csv. Which Newman command correctly achieves this?Solution
Step 1: Identify the option for data files in Newman
The-dor--iteration-dataoption is used to specify a data file for iteration.Step 2: Differentiate environment and data options
-eor--envis for environment files, not data files.Final Answer:
newman run collection.json -d data.csv -> Option AQuick Check:
Use -d for data files in Newman [OK]
- Using -e or --env for data files
- Using --data, an invalid flag
- Confusing environment and data files
Ready to prove it?
Pass the test and this pattern moves unknown
