Postman and automation
Postman is explained as one chain, from variables and scripts to the Runner and Newman in CI, where each link solves the previous one's problem. When variable names clash, the narrower scope wins: Local, Data, Environment, Collection, Global. Newman returns a non-zero exit code when a test fails, so the build turns red without extra scripts, and secrets are passed with --env-var.
API · Interview questions in brief
From variables to CI: how do you tell the Postman story as one connected chain?
Questions about Postman rarely come alone: after "what variable scopes are there" comes "how do you pass a token between requests", then "how do you run it on data" and "how does it end up in CI". A candidate who answers each separately sounds like a list of features. One who holds a single story from a variable to a pipeline sounds like someone who built it. The lesson gives that story and a table of short answers to go with it.
How the chain works and why
Each link solves the problem of the previous one. A hard-coded address and token break when the environment changes, so variables appear. The token exists only after login, so scripts are needed to save a value from the response. One scenario for ten users without copying requests is a Runner with a data file. And so that none of this depends on someone remembering to press a button, Newman runs the collection in the pipeline and returns an exit code.
The chain from a variable to a pipeline
| Question | Short answer |
|---|---|
| Variable scopes in Postman? | Global, Collection, Environment, Data, Local. With equal names the narrower scope wins: Local over Data, Data over Environment, Environment over Collection, Collection over Global. |
| How do you pass data between requests? | Save the value in post-response via pm.environment.set, use {{name}} in the next request. |
| Pre-request vs post-response? | Pre-request prepares the request: data, a signature, a token. Post-response checks the response and saves values. |
| How do you run a collection in CI? | Export the collection and the environment, run newman run with the JUnit reporter; pass secrets through --env-var from CI variables. |
| What do you check besides the status? | The schema, field types and values, headers, response time, absence of sensitive data and that the change was really saved. |
Worked example: the token is not the one you expected
The Login request saves the token in post-response, the next requests take it as {{token}}:
// post-response of the Login request
const body = pm.response.json();
pm.test('has a token', () => pm.expect(body.token).to.be.a('string'));
pm.collectionVariables.set('token', body.token);
// in the next request: Authorization: Bearer {{token}}
The build in CI fails with 401 although everything is green locally. The cause is scopes: the environment file for CI already holds a token variable with an old value, and the script writes the new value into a collection variable (pm.collectionVariables.set), which has lower priority. The narrower scope, Environment, overrides it, and the requests go with the old token. Diagnosis: compare in the Postman console or in the Newman output the value that was substituted with what Login returned. Fix: store into the scope the request reads (pm.environment.set) and do not keep the token in the environment file at all.
The run in the pipeline:
newman run orders.postman_collection.json \
-e staging.postman_environment.json \
-d users.csv \
-r cli,junit --reporter-junit-export results.xml \
--env-var "apiKey=$API_KEY"
Newman returns a non-zero exit code if at least one test of the collection failed, so the build step turns red without extra scripts. CI shows the JUnit file as a test report.
Limits and alternatives
Postman or code: two legitimate options
Exploration and quick checks
Quick to assemble a request, look at the response, show a colleague. Fits a small smoke set and manual scenarios.
A large regression suite
Review in git, types, shared helpers, running next to UI tests. Changes to tests are visible in a diff.
- Newman remains an open-source tool, and Postman also has its own CLI; for an interview it is enough to understand that a collection runs from the command line without the UI.
- Postman scripts are hard to review and reuse: the logic lives inside the collection JSON.
- Secrets in an exported environment end up in git unless removed before the export.
Common mistakes
- A hard-coded host and token in requests: the collection works only on one environment.
- Checking only the status: a 200 with an empty or wrong body passes.
- Shared variables between Runner iterations without cleanup: the second data row sees leftovers of the first.
- Exporting an environment with real secrets into the repository.
Check yourself
A token variable exists both in the collection and in the environment with different values. Which one goes into the request?
Answer
The environment: it is narrower than the collection and wins. So a script that writes the token into a collection variable has no effect while the environment holds a token.
The test is green in Postman and red in Newman in CI. Name two causes you check first.
Answer
Different environments or variable values (the wrong environment file, a secret not passed via --env-var) and the order or dependency between requests (the collection ran partially or without the data file). Then I read the Newman log and compare the substituted values.
--env-var, the JUnit report and exit code as a build gate. To "Postman or code" I answer that Postman is for exploration and a small smoke set, while a large regression stays in code with review in git.