0
0
Postmantesting~10 mins

Newman in CI/CD pipelines in Postman - Test Execution Trace

Choose your learning style9 modes available
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.

Test Code - PyTest
Postman
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()
Execution Trace - 6 Steps
StepActionSystem StateAssertionResult
1Test startsTest runner initializes the test environment-PASS
2Runs Newman command to execute Postman collection with JSON reporterNewman runs the collection and outputs JSON report to stdout-PASS
3Parses Newman JSON report from command outputReport JSON is loaded into memory for analysis-PASS
4Checks that there are zero failures in the Newman reportFailures list is emptyassertEqual(len(failures), 0)PASS
5Verifies that the 'Get User' request returned HTTP status code 200'Get User' request response code is 200assertEqual(response_code, 200)PASS
6Test ends successfullyAll assertions passed, test completes-PASS
Failure Scenario
Failing Condition: Newman reports one or more failed requests in the collection run
Execution Trace Quiz - 3 Questions
Test your understanding
What does the test verify after running Newman?
AThat there are no failed requests in the Postman collection run
BThat Newman installed correctly
CThat the CI/CD pipeline triggered the test
DThat the Postman app is open
Key Result
Always parse and assert Newman JSON reports in CI/CD to catch API failures early and keep tests automated and reliable.