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

API › Runner, data-driven tests and Newman

Newman and CI

Newman runs the same Postman collection from the command line without a UI and exits with a code that any CI understands. The code is zero when everything passed and non-zero when a test failed or a runtime error occurred, so the pipeline step turns red with no extra setup. Secrets are passed with --env-var from the CI secret store, and the report is kept as an artifact.

API · Runner, data-driven tests and Newman

The collection is green in Postman but no pipeline runs it. How do you hook it into CI?

A collection is written in Postman, run by hand before a release, and that is how it lives. API regression stays the job of one person with an open app, and the pipeline knows nothing about it. Newman fixes that: the same collection file runs from the command line without a UI, and the result becomes an exit code that any CI understands.

How it works and why it is designed so

Newman is a CLI runner of collections. The collection and the environment are exported to JSON and put into the repository (or taken from the same branch as the code). Newman runs requests and scripts by the same rules as the Runner and then exits with a code: zero if everything passed and non-zero if at least one test failed or an execution error happened. CI does not read the log: it looks at the exit code, so the step turns red with no extra setup.

From Newman to a red CI step

newman run collection + environment + data Exit code 0 if all passed; not 0 if a test failed The CI step turns red with no extra setup; JUnit and HTML reports
Figure 1. CI does not read the log, it looks at the exit code, so it must not be suppressed.

Example: from a manual run to a pipeline step

Before: the collection is run by hand before a release and nobody keeps a report. After: one command in the pipeline with an environment, data and three reporters:

npm i -g newman@6 newman-reporter-htmlextra@1
newman run bank-api.postman_collection.json \
  -e stage.postman_environment.json \
  -d transfers.csv \
  --env-var "password=$API_PASSWORD" \
  -r cli,htmlextra,junit

The cli reporter writes a log for humans. junit creates a machine report that Jenkins, GitLab and GitHub tooling show as separate tests. htmlextra (a separate package) gives HTML with raw requests and responses: when something fails overnight you see what was sent and received without a rerun. The report is worth keeping as a job artifact. The failure summary looks roughly like this:

# a simplified result (not a verbatim log)
POST /transfers - 201 created           [OK]
POST /transfers - 422 amount 0          [FAILED]
assertions: 12 total, 11 passed, 1 failed
exit code: 1

Secrets are passed through --env-var from the CI secret store and are not kept in the environment file that sits in git. Postman offers one more path of its own, the Postman CLI (postman collection run); flags, reports and a comparison of the two tools are covered in the lesson on Newman in CI.

Limits and alternatives

Newman reproduces the Runner, not the whole app. It has no Postman Vault and cannot see your laptop's local values: everything needed must come from the environment file or --env-var. It is updated more slowly than the app, so the newest script features may not work there: check the collection on the Newman version that sits in CI. For a large regression set with logic and review in git the same job is more convenient in code (the lesson on Postman's limits).

Common mistakes

  • A secret in the environment file in the repository. Pass it through --env-var from the CI store;
  • Not keeping the report. A night failure without an HTML report has to be reproduced;
  • Not pinning versions. A new Newman or reporter release breaks the pipeline with no change in the collection;
  • Running unsafe requests against production. CI needs a separate environment with test data.

Check yourself

How does CI learn that a test in the collection failed, if Newman returns nothing but a log?

Answer

From the exit code: Newman exits with a non-zero code when a test fails or an execution error occurs, and the step turns red. The log is not needed for that.

Why is the password passed through --env-var instead of written into the environment file?

Answer

The environment file sits in git, so the password would reach the history. --env-var takes the value from the CI secret store at run time and leaves nothing in the repository.

In an interview. Junior: In CI I run the collection through newman run with an exported environment and a JUnit reporter, and pass secrets through --env-var. A failed test gives a non-zero exit code and the pipeline turns red. Middle/Senior: I keep the HTML report as an artifact, pin the Newman and reporter versions and keep a separate CI environment with test data. I remember that Newman has no Vault and lags behind the app, so I verify the collection on the pipeline's version.

More articles in this chapter