Computers

Postman CLI (Newman)

Postman CLI (Newman)

Newman is a command-line interface (CLI) for Postman. It allows you to run Postman collections directly from the command line, making it ideal for integrating API testing into automated workflows such as CI/CD pipelines.

Why Use Newman?

Newman offers several advantages:

  • Automation: Run Postman collections as part of automated scripts or workflows.
  • Integration: Seamlessly integrate Postman tests into CI/CD tools like Jenkins, GitHub Actions, or Azure DevOps.
  • Flexibility: Customize request execution, set environment variables, and generate detailed reports.

Installing Newman

Newman is a Node.js package and requires Node.js to be installed on your system.

# Install Newman globally
npm install -g newman

Running a Collection with Newman

To run a Postman collection using Newman:

  • Export your collection from Postman as a JSON file.
  • Run the following command in your terminal:
newman run 

Example:

newman run my-collection.json

Using Environments with Newman

Newman supports environment files exported from Postman. Use the --environment flag to specify an environment file:

newman run  --environment 

Advanced Options

  • Iteration Data: Provide a CSV/JSON file to run the collection with different sets of data:
    newman run  --iteration-data
  • Reporters: Generate detailed test reports using built-in or custom reporters:
    newman run  --reporters cli,json
  • Timeouts: Set request and script timeouts:
    newman run  --timeout 5000

Integrating Newman with CI/CD

Newman can be integrated into CI/CD tools to automate API testing:

  • Jenkins: Add a shell/batch script step to execute Newman commands.
  • GitHub Actions: Use Newman as part of a workflow to test APIs after code pushes.
  • Docker: Run Newman in a Docker container for consistent testing environments.

Example CI/CD Command

newman run my-collection.json --environment my-environment.json
 --reporters cli,junit --reporter-junit-export results.xml