Run a collection
Introduction
Newman is Postman's command-line collection runner for exported v2.1 collections. Postman recommends the newer Postman CLI for new scripts.
- Newman README (github.com)
- Newman CLI documentation (learning.postman.com)
- Collection data files (learning.postman.com)
- Migrating to the Postman CLI (learning.postman.com)
First run
Command
$ newman run collection.json -e env.json \
--reporter-cli-no-summary
Output
newman
Users API
→ Get user
GET http://127.0.0.1:3210/users/1 [200 OK, 176B, 26ms]
✓ status is 200
✓ user has a name
❏ Health
↳ Health check
GET http://127.0.0.1:3210/health [200 OK, 166B, 2ms]
-e supplies an exported environment. The summary flag keeps this example short; drop it for the statistics table.
See: Newman CLI docs
Select a folder
Command
$ newman run collection.json -e env.json \
--folder Health --reporter-cli-no-summary
Output
newman
Users API
❏ Health
↳ Health check
GET http://127.0.0.1:3210/health [200 OK, 166B, 24ms]
Repeat --folder to run multiple folders or individual requests.
See: Command-line options
Inline variables
Command
$ newman run collection.json \
--env-var baseUrl=http://127.0.0.1:3210 \
--env-var userId=2 \
--reporter-cli-no-summary
Output
newman
Users API
→ Get user
GET http://127.0.0.1:3210/users/2 [200 OK, 178B, 39ms]
✓ status is 200
✓ user has a name
❏ Health
↳ Health check
GET http://127.0.0.1:3210/health [200 OK, 166B, 2ms]
CLI variables override matching environment variables.
See: Command-line options
Global variables
$ newman run collection.json -g globals.json -e env.json
Globals have lower precedence than environment variables. Use --global-var key=value to set one inline.
See: Command-line options
Iterations
Repeat a run
Command
$ newman run collection.json -e env.json \
--folder 'Get user' -n 2 --reporter-cli-no-summary
Output
newman
Users API
Iteration 1/2
→ Get user
GET http://127.0.0.1:3210/users/1 [200 OK, 176B, 26ms]
✓ status is 200
✓ user has a name
Iteration 2/2
→ Get user
GET http://127.0.0.1:3210/users/1 [200 OK, 176B, 2ms]
✓ status is 200
✓ user has a name
-n sets the iteration count; --folder narrows the output to one request.
See: Command-line options
Data-driven runs
Data file
[
{ "userId": "1" },
{ "userId": "2" }
]
Command
$ newman run collection.json -e env.json \
--folder 'Get user' -d data.json --reporter-cli-no-summary
Output
newman
Users API
Iteration 1/2
→ Get user
GET http://127.0.0.1:3210/users/1 [200 OK, 176B, 24ms]
✓ status is 200
✓ user has a name
Iteration 2/2
→ Get user
GET http://127.0.0.1:3210/users/2 [200 OK, 178B, 2ms]
✓ status is 200
✓ user has a name
Each row supplies one iteration; {{userId}} reads its userId field.
See: Data files
CSV data
$ newman run collection.json -e env.json -d data.csv
CSV headers become variable names, so each row can supply {{userId}} and more.
See: Data files
Reports and exports
Built-in reporters
$ newman run collection.json -e env.json \
-r cli,json,junit
Built-in reporters are cli, json, junit, progress, and emojitrain. Explicit -r disables the CLI reporter unless cli is included.
See: Reporters
JSON report
$ newman run collection.json -e env.json \
-r cli,json \
--reporter-json-export run.json
Without a path, Newman writes the report into newman/ in the working directory.
See: JSON reporter
JUnit report
$ newman run collection.json -e env.json \
-r junit \
--reporter-junit-export run.xml
The XML groups assertions by name into one testcase per named test in each request, ready for a CI dashboard.
See: JUnit reporter
Tune CLI output
$ newman run collection.json -e env.json \
--reporter-cli-show-timestamps \
--reporter-cli-no-assertions
Other switches include --reporter-cli-no-summary, --reporter-cli-no-console, and --reporter-cli-no-banner.
See: CLI reporter
Export run state
$ newman run collection.json -e env.json \
--export-environment final-env.json
Also available: --export-globals, --export-collection, and --export-cookie-jar.
See: Command-line options
CI and automation
Stop on first failure
$ newman run collection.json -e env.json \
--bail failure
Failed assertions exit 1; --bail failure stops after the current test script. --bail folder aborts the run when a folder is invalid.
See: Newman in CI
Suppress the exit code
$ newman run collection.json -e env.json \
-x
-x (--suppress-exit-code) exits 0 even when tests fail.
See: Command-line options
Quiet output
$ newman run collection.json -e env.json \
--silent
--silent hides all output; --color off removes ANSI colors.
See: Command-line options
Connection options
TLS options
$ newman run collection.json -e env.json -k
-k skips certificate validation. Client certificates use --ssl-client-cert and --ssl-client-key.
See: SSL
Timeouts and pacing
$ newman run collection.json -e env.json \
--timeout-request 5000 \
--delay-request 100
Durations are milliseconds. --timeout caps the whole run and --timeout-script caps scripts.
See: Command-line options
Integrations
Node.js library
const newman = require('newman')
newman.run(
{
collection: require(
'./collection.json'
),
environment: require('./env.json'),
reporters: 'cli'
},
(err, summary) => {
if (err) throw err
console.log(
summary.run.stats.assertions.total
) // 2
}
)
The callback receives a run summary with stats, assertions, and failures.
See: Node library
Docker
$ docker run --rm -v "$PWD:/etc/newman" \
-t postman/newman \
run collection.json -e env.json
Mount the folder holding the collection and environment at /etc/newman.
See: Docker image
From the Postman API
$ newman run "https://api.getpostman.com/\
collections/$UID" \
--postman-api-key "$POSTMAN_API_KEY"
Fetch collection and environment URLs from the Postman API; Newman accepts either as a source.
See: Postman API
0 Comments for this cheatsheet. Write yours!