import subprocess
# Define paths to collection and environment files
collection_file = 'MyCollection.json'
environment_file = 'MyEnvironment.json'
# Run Newman command with environment file
result = subprocess.run([
'newman', 'run', collection_file,
'-e', environment_file
], capture_output=True, text=True)
# Print Newman output
print(result.stdout)
# Assert that Newman run was successful
assert result.returncode == 0, f"Newman run failed with exit code {result.returncode}"This script uses Python's subprocess module to run the Newman CLI command that executes a Postman collection with a specified environment file.
The newman run command includes the -e option to specify the environment file, ensuring environment variables are loaded.
The script captures the output and prints it, so you can see the test run results in the console.
Finally, it asserts that the Newman command exited with code 0, which means the run was successful.
This approach automates running the collection with environment variables and verifies the execution result.