Newman - Postman CLI Runner

6.2.2 · active · verified Tue Apr 21

Newman is a powerful command-line collection runner for Postman, enabling users to execute and test Postman collections directly from the command line without the Postman GUI. It is primarily designed for integration into continuous integration and continuous delivery (CI/CD) pipelines and other automated build systems to facilitate automated API testing. The current stable version is 6.2.2. While there isn't a fixed release cadence, the project is actively maintained, with major versions typically introducing Node.js compatibility updates and dependency upgrades (e.g., v6 was released in late 2023). Key differentiators include its seamless integration with Postman collections, support for various output reporters (CLI, JSON, JUnit, HTML), and its capability for programmatic use as a Node.js library, offering a robust solution for automated API testing workflows.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to programmatically run a Postman collection using Newman as a Node.js library, including specifying reporters and handling the run summary for success or failure states. It generates a temporary collection file and cleans it up after execution.

import newman from 'newman';
import path from 'path';
import fs from 'fs';

// Create a dummy collection file for demonstration
const dummyCollectionPath = path.resolve(__dirname, 'dummy-collection.json');
const dummyCollectionContent = {
  "info": {
    "_postman_id": "b8c5f2b8-f3d9-4b6a-8b1b-c7e1e4f9b8c2",
    "name": "Dummy API Tests",
    "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
  },
  "item": [
    {
      "name": "Test Get Request",
      "request": {
        "method": "GET",
        "header": [],
        "url": {
          "raw": "https://postman-echo.com/get?foo=bar",
          "protocol": "https",
          "host": ["postman-echo", "com"],
          "path": ["get"],
          "query": [
            {
              "key": "foo",
              "value": "bar"
            }
          ]
        }
      },
      "response": []
    }
  ]
};

fs.writeFileSync(dummyCollectionPath, JSON.stringify(dummyCollectionContent, null, 2));

console.log(`Running collection: ${dummyCollectionPath}`);

newman.run({
  collection: dummyCollectionPath,
  reporters: ['cli', 'json'],
  reporter: {
    json: {
      export: './newman-report.json'
    }
  },
  environment: {  // Example of passing environment variables programmatically
    values: [
      { key: 'baseUrl', value: 'https://postman-echo.com', enabled: true }
    ]
  }
}, (err, summary) => {
  // Clean up dummy file after run
  fs.unlinkSync(dummyCollectionPath);

  if (err) {
    console.error('Collection run encountered an error:', err);
    return;
  }

  if (summary.run.failures.length) {
    console.error('Collection run completed with failures:');
    summary.run.failures.forEach(failure => {
      console.error(`  - ${failure.error.message} in ${failure.source.name}`);
    });
  } else {
    console.log('Collection run completed successfully!');
    console.log(`Total requests: ${summary.run.stats.requests.total}`);
    console.log(`Total assertions: ${summary.run.stats.assertions.total}`);
    console.log(`Assertions failed: ${summary.run.stats.assertions.failed}`);
    console.log('JSON report generated at ./newman-report.json');
  }
});

view raw JSON →