How to Use Newman in CI/CD with Postman for Automated Testing
Use
Newman, Postman's command-line tool, to run collections in your CI/CD pipeline by installing it via npm and executing tests with commands like newman run collection.json. Integrate this command into your CI/CD scripts (e.g., GitHub Actions, Jenkins) to automate API testing during builds.Syntax
The basic syntax to run a Postman collection using Newman is:
newman run <collection-file>: Runs the specified Postman collection JSON file.--environment <env-file>: Optional flag to specify an environment file.--reporters <reporter-names>: Optional flag to specify output formats likecli,json, orhtml.--delay-request <ms>: Optional delay between requests in milliseconds.
This command is used inside your CI/CD pipeline scripts to automate API tests.
bash
newman run collection.json --environment environment.json --reporters cli,json,html
Example
This example shows how to run a Postman collection named my_collection.json with an environment file my_env.json and generate CLI and JSON reports. This command can be added to your CI/CD pipeline script to automate tests.
bash
npm install -g newman
newman run my_collection.json --environment my_env.json --reporters cli,json --reporter-json-export results.jsonOutput
newman
my_collection
โ GET /users 200 OK
โ POST /users 201 Created
2 passing (1s)
Common Pitfalls
Common mistakes when using Newman in CI/CD include:
- Not installing Newman globally or locally before running commands.
- Using incorrect paths for collection or environment files, causing file not found errors.
- Failing to fail the CI build on test failures by not checking Newman exit codes.
- Not generating or saving reports for test results analysis.
Always ensure Newman is installed, file paths are correct, and your CI script fails on test errors.
bash
Wrong way:
newman run wrong_path/collection.json
Right way:
npm install -g newman
newman run correct_path/collection.json --exit-code
# The --exit-code flag makes Newman return a non-zero exit code on test failures, causing CI to fail.Quick Reference
| Command/Flag | Description |
|---|---|
| newman run | Run a Postman collection file |
| --environment | Specify environment variables file |
| --reporters cli,json,html | Generate test reports in CLI, JSON, and HTML formats |
| --reporter-json-export | Save JSON report to a file |
| --exit-code | Return non-zero exit code on test failures to fail CI build |
| npm install -g newman | Install Newman globally |
Key Takeaways
Install Newman globally with npm before using it in CI/CD pipelines.
Use the 'newman run' command with collection and environment files to automate tests.
Include the '--exit-code' flag to ensure CI fails on test failures.
Generate and save reports for test result analysis in CI/CD.
Verify file paths and dependencies to avoid common errors.