Newman - Postman CLI Runner
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
-
'newman' is not recognized as an internal or external command, operable program or batch file.
cause Newman is either not installed globally, or its npm global bin directory is not in your system's PATH environment variable.fixInstall Newman globally: `npm install -g newman`. If already installed, ensure `$(npm config get prefix)/bin` (or `C:\Users\<YourUser>\AppData\Roaming\npm` on Windows) is in your system's PATH. -
Cannot find module 'newman' Require stack:
cause You are trying to use Newman as a library in a Node.js project, but it's not installed as a local dependency or linked correctly.fixInstall Newman as a local project dependency: `npm install newman`. If using TypeScript, also install types: `npm install @types/newman --save-dev`. -
Error: unable to open collection file "<filename.json>".
cause The specified collection file path is incorrect, the file does not exist at that location, or there are file permission issues.fixVerify the file path and name are correct. Ensure the user running Newman has read permissions for the file. Use absolute paths, especially in CI/CD environments. -
Error: Node.js v<X> is not supported. Please upgrade to Node.js v16 or higher.
cause You are running Newman v6 or newer on an unsupported Node.js version.fixUpgrade your Node.js environment to version 16 or later. For example, use `nvm install 16` and `nvm use 16`, or update your system's Node.js installation.
Warnings
- breaking Newman v6.0 and newer versions require Node.js v16 or higher. Older Node.js versions (e.g., v10, v12, v14) are no longer supported. Attempting to run Newman v6 on an unsupported Node.js version will result in runtime errors.
- breaking Newman v6 includes significant dependency upgrades, particularly for the Postman Runtime and `tough-cookie`. These updates were made to address security vulnerabilities and introduce new features like JWT and NTLMv2 authentication.
- gotcha Newman does not natively support OAuth 2.0 authentication directly within collections. While Postman can handle OAuth flows, when running with Newman, you'll need to manually manage and provide access tokens (e.g., via environment variables or pre-request scripts) to your requests.
- gotcha When running Newman in CI/CD environments, ensure that collection, environment, and data files are accessible via absolute or correct relative paths. Path inconsistencies are a common source of 'file not found' errors, especially when the working directory differs from local development.
- gotcha SSL certificate verification can cause issues with self-signed certificates or specific corporate proxies. Newman offers an `--insecure` option to bypass SSL certificate validation, but use it with caution in production environments.
Install
-
npm install newman -
yarn add newman -
pnpm add newman
Imports
- newman
import { newman } from 'newman'; const newman = require('newman').default;import newman from 'newman';
- NewmanRunOptions
import { NewmanRunOptions } from 'newman'; import type NewmanRunOptions from 'newman';import type { NewmanRunOptions } from 'newman'; - NewmanRunSummary
import type { NewmanRunSummary } from 'newman';
Quickstart
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');
}
});