QA Interview Hub · Knowledge base · Quizzes · Українська

API › Runner, data-driven tests and Newman

Newman in CI: flags, reports, exit codes, Postman CLI

Newman exits with a non-zero code when at least one test fails, so the CI step turns red by itself. The exit code is not silenced with || true or a pipe without pipefail. The cli reporter prints to the console, while junit and htmlextra write files that are stored as an artifact with if: always(). The official Postman CLI (postman collection run) also runs collections by ID from Postman.

API · Runner, data-driven tests and Newman

Newman failed overnight and there is no report among the artifacts. What is missing in the step?

The night job failed and in the morning you open the artifacts and there is no report: the upload step did not run because the previous one was red. Another time it is the opposite: tests failed for a week while the pipeline stayed green because someone appended || true so it would "not get in the way". Both cases are about the same thing: what happens to Newman's exit code and where the report lives. The lesson covers flags, reporters, the exit code and the Postman CLI.

How it works and why it is designed so

The exit code. Everything passed: 0. If at least one test fails (or an execution error occurs), Newman exits with a non-zero code and the CI step turns red. So you do not need to check the code separately, but you must not suppress it: the pipeline would be always green. The same goes for the pipe newman run … | tee log: in bash without pipefail the pipe's code is the code of the last command, so Newman's failure disappears. In GitHub Actions a step with an explicit shell: bash runs with pipefail and without it does not, so for a log prefer a reporter.

What happens to the exit code

Wrong

The exit code is lost

newman run … || true or a pipe | tee log without pipefail: the step is green although tests failed.

Right

The exit code reaches CI

Run Newman without suppression and use a reporter for the log, not redirection.

Figure 1. A red step works only when Newman’s exit code reached CI.

Main flags. The ones met most often in CI:

  • -e env.json, -g globals.json, -d data.csv: environment, globals, data for iterations;
  • --env-var "key=value": one variable from the command line, the way secrets are passed;
  • --folder "Name": only the given folder (the flag can be repeated); -n (--iteration-count): the number of iterations;
  • --bail: stop after the first failure, otherwise Newman goes through the whole collection;
  • --delay-request 200: a pause between requests; --timeout-request 10000: a response wait limit;
  • -r cli,junit,htmlextra: reporters, each with its own export file (--reporter-junit-export, --reporter-htmlextra-export).

Which reporters for what. cli: a log for humans in the CI console. junit: a machine format that Jenkins, GitLab and GitHub tooling understand to show tests in the interface. htmlextra (a separate package newman-reporter-htmlextra): HTML with raw requests and responses for analysing failures.

Example: one command with all the flags

# environment, data and iterations, one folder, secret from CI;
# --bail stops the run at the first failure
newman run api.postman_collection.json \
  -e stage.postman_environment.json \
  -d transfers.csv -n 5 \
  --folder "Transfers" \
  --env-var "apiKey=$API_KEY" \
  --delay-request 200 --timeout-request 10000 \
  --bail \
  -r cli,junit,htmlextra \
  --reporter-junit-export results/newman.xml \
  --reporter-htmlextra-export results/report.html

In GitHub Actions the same run looks like this. if: always() is needed so the report is saved exactly when tests fail: otherwise there is no artifact when it is needed most.

steps:
  - uses: actions/checkout@v7
  - uses: actions/setup-node@v7
    with:
      node-version: 24
  - run: npm i -g newman@6 newman-reporter-htmlextra@1
  - name: API regression
    run: >
      newman run api.postman_collection.json
      -e stage.postman_environment.json
      --env-var "apiKey=${{ secrets.API_KEY }}"
      -r cli,junit,htmlextra
      --reporter-junit-export results/newman.xml
      --reporter-htmlextra-export results/report.html
  - uses: actions/upload-artifact@v7
    if: always()          # the report is needed exactly when tests fail
    with:
      name: newman-report
      path: results/

The Postman CLI. This is Postman's official tool: after signing in with an API key, postman collection run runs a collection from the cloud workspace by ID, and a local collection file as well.

# Postman CLI: sign in with an API key, then run by collection ID
postman login --with-api-key "$POSTMAN_API_KEY"
postman collection run <collection-id>

# a local collection file can be run without signing in too
postman collection run ./api.postman_collection.json

Newman or the Postman CLI

Newman

A collection file in the repository

A run from v2.1 JSON in the repository with no login, many reporters, the version is pinned by the repository.

Postman CLI

A collection in the cloud workspace

A run by ID with the current version and no export; results are visible in Postman; an API key is required.

Figure 2. The choice depends on where the collection lives: in the repository or in the cloud workspace.

Limits and alternatives

Run hygiene: pin the versions of Newman and reporters, do not run a collection with unsafe writes against production, keep the CI environment separate and with test data. Newman does not support the Vault, and newer script features can appear in the app before they reach Newman: one more argument to verify the collection on the pipeline's version. The choice between Newman and the Postman CLI depends on where the collection lives: a file in the repository gives a reproducible run together with the code, while the cloud workspace gives an always current version without an export. Newman reads only v2.1 JSON; a collection in the new YAML format (a file per request, Native Git) runs only in the Postman CLI.

Common mistakes

  • || true or | tee without pipefail. The pipeline is green while tests are red;
  • A report without if: always(). There is no artifact exactly when it is needed;
  • One export file for two reporters. Each reporter needs its own path;
  • The --bail flag where cleanup is needed. The run stops and Cleanup does not execute;
  • Unpinned versions. A new release breaks the pipeline with no change in the collection.

Check yourself

The pipeline has newman run … || true. What follows from that?

Answer

Newman's exit code is ignored, so the step is always green even when tests fail. Remove || true and keep the report with a separate step using if: always().

Why three reporters cli,junit,htmlextra and why different files?

Answer

cli for a human in the log, junit for a machine display of tests in CI, htmlextra for failure analysis with raw requests. The files differ because each reporter writes to its own export.

In an interview. Junior: I run newman run with an environment, JUnit and HTML reporters and pass secrets through --env-var. I keep the report as an artifact with if: always() and I do not suppress the exit code. Middle/Senior: I pin Newman and reporter versions, keep a separate CI environment with test data and make sure pipes do not lose the exit code (pipefail). I choose Newman or the Postman CLI by where the collection lives: a file in the repository or the cloud workspace.

More articles in this chapter