Test Overview
This test runs a Postman collection using Newman in a CI/CD pipeline. It verifies that the API responses meet expected status codes and data values.
This test runs a Postman collection using Newman in a CI/CD pipeline. It verifies that the API responses meet expected status codes and data values.
import subprocess import json import unittest class TestNewmanInCICD(unittest.TestCase): def test_postman_collection(self): # Run Newman command to execute Postman collection result = subprocess.run([ 'newman', 'run', 'https://api.getpostman.com/collections/your-collection-id', '--environment', 'https://api.getpostman.com/environments/your-environment-id', '--reporters', 'json' ], capture_output=True, text=True) # Parse Newman JSON report from stdout report = json.loads(result.stdout) # Check that all requests passed failures = report['run']['failures'] self.assertEqual(len(failures), 0, f"Failures found: {failures}") # Check a specific request response code for execution in report['run']['executions']: if execution['item']['name'] == 'Get User': self.assertEqual(execution['response']['code'], 200) if __name__ == '__main__': unittest.main()
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts | Test runner initializes the test environment | - | PASS |
| 2 | Runs Newman command to execute Postman collection with JSON reporter | Newman runs the collection and outputs JSON report to stdout | - | PASS |
| 3 | Parses Newman JSON report from command output | Report JSON is loaded into memory for analysis | - | PASS |
| 4 | Checks that there are zero failures in the Newman report | Failures list is empty | assertEqual(len(failures), 0) | PASS |
| 5 | Verifies that the 'Get User' request returned HTTP status code 200 | 'Get User' request response code is 200 | assertEqual(response_code, 200) | PASS |
| 6 | Test ends successfully | All assertions passed, test completes | - | PASS |