Notes
Collections
A collection is a group of saved requests, organised in folders, each with its own tests. For ShopRung: an auth folder with a good login and a wrong password, a cart folder for adding items, an orders folder for placing and cancelling, and a reset request. Collections export as JSON, so they can live in Git next to your code and be reviewed like any other test code. That is the first habit to build: never keep tests only inside the app.
- ShopRung API Auth Login (alice) Login wrong password Cart Add item Add quantity (data-driven) Orders Place order Cancel shipped order Reset
Variables
Variables keep a collection reusable. An environment is a set of variables: in the local one, base URL is localhost port eight thousand and eighty; in staging, it is the staging address. Requests use double curly braces: base URL slash API slash cart slash items, and an authorization header with bearer and the token variable. Switch environments, and the same collection tests a different server. Mark secret values as secret and never commit real ones.
local.postman_environment.json
baseUrl = http://localhost:8080
staging environment
baseUrl = https://staging...
request URL:
{{baseUrl}}/api/cart/items
header:
Authorization: Bearer {{token}}
secrets: mark as secret, never
commit real values Saving a token
Each request can run a script after the response arrives, in its tests tab, written in JavaScript. The login request checks the status is two hundred, reads the JSON body, and stores the token in an environment variable. Every later request sends it automatically through the authorization header. This chaining, using one response in the next request, is how collections cover real flows: log in, add to cart, place an order, then cancel it.
pm.test("status is 200", () =>
pm.response.to.have.status(200));
const body = pm.response.json();
pm.environment.set("token",
body.token);
// later requests send
// Authorization: Bearer {{token}} Assertions
Assertions use pm dot test with a name and a function, and pm dot expect, which reads almost like English. Check the status is two hundred and one. Check a value in the body, like a discount of exactly five hundred cents. Check a header is present. Check the response time is below five hundred milliseconds. Name each test after the rule it checks, because the names become the report everyone reads.
pm.test("201 Created", () =>
pm.response.to.have.status(201));
pm.test("discount is $5.00", () =>
pm.expect(pm.response.json()
.discountCents).to.eql(500));
pm.test("JSON", () => pm.response
.to.have.header("Content-Type"));
pm.test("fast", () => pm.expect(
pm.response.responseTime)
.to.be.below(500)); Data-driven
Data driven runs turn one request into many tests. A CSV file holds the quantity boundaries from rule cart two: zero gives four hundred, one and ten give two hundred, eleven gives four hundred. The request body uses the qty variable, and the test reads the expected status from the same row. The collection runner, or Newman with dash d, runs the collection once per row. Four rows, four iterations, every boundary covered.
qty,status
0,400
1,200
10,200
11,400
body: {"productId":1,
"quantity":{{qty}}}
pm.test(`qty ${qty}`, () =>
pm.response.to.have.status(
Number(status))); Newman
Newman is Postman's command line runner. Install it with npm, then run the exported collection with the environment file and the data file. Against ShopRung this run made four iterations, twelve requests and eight assertions, all passed, in about seven tenths of a second. Switch on the quantity limit bug and the qty eleven row fails, with the expected and actual status in the output. Postman also offers its own Postman CLI that works similarly.
npm install -g newman newman run shoprung.json -e local.json -d quantities.csv ✓ status is 200 ✓ qty 0 → 400 ✓ qty 1 → 200 ✓ qty 10 → 200 ✓ qty 11 → 400 iterations 4, requests 12, assertions 8, failed 0 total run duration: ~0.7 s
In CI
In CI, the pipeline starts ShopRung, waits until the health check answers, then runs Newman. The junit reporter writes an XML report that CI tools display as test results, and community reporters can produce HTML. A failed assertion makes Newman exit with an error, which fails the job, so a broken rule blocks the merge. Part eight builds this pipeline in GitHub Actions.
- run: npm start &
working-directory: shoprung
- run: npx wait-on
http-get://localhost:8080/api/health
- run: npx newman run shoprung.json
-e ci.json -d quantities.csv
-r cli,junit
--reporter-junit-export
results/newman.xml
# a failed assertion fails the job Where it fits
Where does Postman fit? It is excellent for exploring a new API, sharing requests with developers, quick smoke suites and mock servers. As suites grow, it gets harder: logic spreads across many small scripts, reviews mean reading JSON diffs, and reuse and refactoring are awkward. Many teams explore in Postman and build their main API suite in code, with REST Assured or pytest, which are the next two episodes.
- Postman strengths explore an API fast share requests with the team quick smoke and contract suites, mock servers
- limits at scale logic in many small scripts reviews of JSON diffs reuse and refactoring → big suites: REST Assured, pytest (next episodes)
Common mistakes
Postman habits that hurt later. Collections that live only in the app, not in Git. Hard coded URLs and tokens instead of variables. Requests that only work if someone clicks them in a certain order. Tests that only check for two hundred. Secrets exported inside the collection JSON. And no reset between runs, so the second run fails because the first one changed the data.
- collections only in the app, not in Git
- hard-coded URLs and tokens
- requests that depend on manual clicking order
- tests that only check 200
- secrets exported in JSON
- no reset between runs
Say it like this
In the interview: I keep collections in Git with an environment per server. The login request stores the token in a variable and later requests chain from it. pm dot test checks status, body, headers and response time, and CSV data files cover boundary values. Newman runs the collection in CI with JUnit reports, and any failed assertion fails the build. For large suites, I move to code based tools like REST Assured.
- "Collections in Git, with environments for each server.
- Login stores the token in a variable; requests chain.
- pm.test checks status, body, headers and time; CSV data
- files cover boundaries.
- Newman runs it in CI with JUnit reports, failing the build on any assertion."