QA and Test Automation · Lab 9 of 35
Postman Collection and Newman in CI: ShopRung API Lab
Build a Postman collection for ShopRung step by step: an environment with variables, a login that saves its token, chained requests through cart and orders, pm.test checks, a data-driven run from a CSV, then Newman on the command line with a JUnit report. Catches all five mystery bugs.
⏱ 60 minBeginner💸 Free8 steps
Postman (free account or the desktop app)Newman 6.2Node.js 22ShopRung
Overview video
What you will build
Two Postman collections, an environment and a data file, exported to a folder you can keep in Git:
- ShopRung API: 13 requests in four folders (Setup, Auth, Cart, Orders) with 19 assertions. A login saves its token, and later requests reuse it; placing an order saves its id for the next request.
- ShopRung quantity limits: one request run once per row of
quantities.csv, covering 0, 1, 10 and 11. - A
package.jsonsonpm testruns everything with Newman and writes a JUnit XML report for CI.
With ShopRung's mystery mode on, the two runs together find all five hidden bugs.
Before you start
- The idea behind this lab: Postman and Newman, plus HTTP and REST for testers and API authentication.
- ShopRung installed, as in lab 1 setup. You have Node.js 22 from that setup, which Newman needs.
- No coding experience needed: the test scripts are short and every line is explained.
Setup
S1Start ShopRung
Start ShopRung as in lab 1 with npm start, and leave it running.
curl http://localhost:8080/api/healthcurl http://localhost:8080/api/healthcurl.exe http://localhost:8080/api/health {"status":"UP","app":"ShopRung","version":"1.0.0"} S2Install Postman
Download the free Postman desktop app from postman.com and sign in (a free account is enough). Use the desktop app, not the browser version: the browser version cannot reach localhost without the Postman agent.
S3Make a project folder
Collections belong in Git next to the code, not only inside the app. Make a folder for the files you will export, and add package.json and .gitignore from step 8 later.
mkdir shoprung-postman
cd shoprung-postman An environment with variables
A ShopRung local environment, so no request contains a hard-coded address or password.
In Postman, click Environments in the left sidebar, then +, and name it ShopRung local. Add the variables below. Mark alicePassword, token and bobToken as type secret, so Postman hides their values on screen.
Select ShopRung local in the environment drop-down at the top right. From now on, {{baseUrl}} in a request means http://localhost:8080. To test another server, you make a second environment with a different baseUrl; the collection does not change.
The file below is what Postman exports later (step 8). You can also import it directly: Import, then choose the file.
{
"id": "shoprung-local",
"name": "ShopRung local",
"values": [
{
"key": "baseUrl",
"value": "http://localhost:8080",
"type": "default",
"enabled": true
},
{
"key": "aliceEmail",
"value": "alice@shoprung.test",
"type": "default",
"enabled": true
},
{
"key": "alicePassword",
"value": "Alice123!",
"type": "secret",
"enabled": true
},
{
"key": "token",
"value": "",
"type": "secret",
"enabled": true
},
{
"key": "bobToken",
"value": "",
"type": "secret",
"enabled": true
},
{
"key": "orderId",
"value": "",
"type": "default",
"enabled": true
}
],
"_postman_variable_scope": "environment"
} - The only place the server address appears.line 6
- Hidden on screen. Real passwords never go into an exported file: keep them in CI secrets.line 20
- Empty for now. The login request fills it in.line 24
Reset and log in, saving the token
A collection with a Setup folder: reset the shop, then log Alice and Bob in and keep their tokens.
Click Collections, then +, and name the collection ShopRung API. Add a folder 0 Setup (the number keeps folders in run order). Inside it, add three requests:
- Reset the shop:
POST {{baseUrl}}/api/test/reset. - Log in as Alice:
POST {{baseUrl}}/api/auth/login. In Body, choose raw and JSON, and enter{"email": "{{aliceEmail}}", "password": "{{alicePassword}}"}. - Log in as Bob: the same with Bob's email and password (
bob@shoprung.test,Bob12345!).
Each request has a Scripts tab with a Post-response section (older versions call it Tests): JavaScript that runs after the response arrives. For Alice's login, paste the script below. It checks the status, then stores the token in the environment. Do the same for Bob, saving to bobToken. Click Send on each request and watch token fill in under the environment.
pm.test('login: 200', () => pm.response.to.have.status(200));
pm.environment.set('token', pm.response.json().token); pm.test(name, fn): a named check that shows as passed or failed.line 1- Chaining: this response feeds the next requests through
{{token}}.line 2
Negative checks: wrong password, no token
An Auth folder with two requests that must be refused.
Add a folder 1 Auth with two requests:
- Wrong password: the login request with password
wrong. Post-response script: the status is401, andpm.response.json().messageis exactly Email or password is wrong. An unknown email gets the same message, so attackers cannot learn who has an account. - Cart without a token:
GET {{baseUrl}}/api/cartwith no Authorization header. It must be401with errorunauthorized.
Name each pm.test after the rule it checks (ACC for accounts, CART, PRICE, ORD), the same ids as in ShopRung's requirements. The names are the report everyone reads.
The cart: SAVE10 on its boundary
A Cart folder: four notebooks, SAVE10 at exactly $50.00, and the expired coupon.
Add a folder 2 Cart. For every request in it, open Headers and add Authorization with the value Bearer {{token}}. (You can also set this once on the collection: Authorization tab, type Bearer Token, token {{token}}, and let requests inherit it.)
- Add 4 notebooks ($50.00):
POST {{baseUrl}}/api/cart/itemswith{"productId": 1, "quantity": 4}. Check status 200 andsubtotalCents5000. - Apply SAVE10 at $50.00:
POST {{baseUrl}}/api/cart/couponwith{"code": "save10"}, lower case on purpose. Paste the script below. - Expired WELCOME15: the same with
WELCOME15. Status400, message This coupon has expired.
const cart = pm.response.json();
pm.test('PRICE: SAVE10 applies at exactly $50.00', () => pm.response.to.have.status(200));
pm.test('discount is $5.00', () => pm.expect(cart.discountCents).to.eql(500));
pm.test('total is $49.99', () => pm.expect(cart.total).to.eql('$49.99'));
pm.test('fast enough', () => pm.expect(pm.response.responseTime).to.be.below(1000)); - Parse the body once, then check several fields.line 1
- Exact cents: 10% of 5000.line 3
- $50.00 - $5.00 + $4.99 shipping.line 4
- A response time check. Keep it loose: it guards against something very wrong, not small changes.line 5
Orders: chain the order id
An Orders folder that places an order, checks a side effect, and uses the saved id.
Add a folder 3 Orders (Authorization Bearer {{token}} again, except where noted):
- Place the order:
POST {{baseUrl}}/api/orderswith no body. Paste the script below: it checks201and PLACED, and saves the new order's id. - Stock went down:
GET {{baseUrl}}/api/products/1(no token needed). The notebook'sstockmust be 36: 40 minus the 4 you ordered. The order response alone cannot tell you this. - Bob opens Alice's order:
GET {{baseUrl}}/api/orders/{{orderId}}withBearer {{bobToken}}. Must be404. - Cancel a shipped order:
POST {{baseUrl}}/api/orders/2/cancel. Alice's seed order 2 is SHIPPED, so:409, Cannot cancel an order that is SHIPPED. - Cancel the order:
POST {{baseUrl}}/api/orders/{{orderId}}/cancel. A PLACED order can be cancelled:200, status CANCELLED.
pm.test('ORD: 201 Created', () => pm.response.to.have.status(201));
pm.test('status is PLACED', () => pm.expect(pm.response.json().status).to.eql('PLACED'));
pm.environment.set('orderId', pm.response.json().id); - The next three requests use
{{orderId}}: the id of the order just made, whatever number it is.line 3
Run the whole collection
The Collection Runner sends all 13 requests in order: 19 checks pass.
Click the collection's ... menu, then Run collection. Keep the order as it is and click Run ShopRung API. Postman sends every request top to bottom and shows each pm.test as passed or failed.
Because the first request resets the shop, you can run it again and again with the same result. That is the most important property of any test suite: it does not depend on what ran before.
Collection Runner: Run ShopRung API 0 Setup / Reset the shop ✓ reset: 200
0 Setup / Log in as Alice ✓ login: 200
...
2 Cart / Apply SAVE10 at $50.00 ✓ PRICE: SAVE10 applies at exactly $50.00
✓ discount is $5.00
✓ total is $49.99
...
3 Orders / Cancel the order ✓ ORD: a PLACED order can be cancelled
Passed 19 Failed 0 Data-driven: one request, four boundaries
A second collection run once per row of a CSV file.
Save quantities.csv below in your project folder. Make a new collection ShopRung quantity limits with three requests: reset, log in as Alice (saving token), and Add quantity from the data file: POST {{baseUrl}}/api/cart/items with the raw body {"productId": 1, "quantity": {{qty}}} and the script below.
In the Runner, click Select File under Data, choose quantities.csv, and run. Postman runs the collection once per row (an iteration), and {{qty}} and pm.iterationData take that row's values. One request, four boundary checks: 0 and 11 refused, 1 and 10 accepted.
qty,status
0,400
1,200
10,200
11,400 - The header names the variables:
{{qty}}andstatus.line 1
const qty = pm.iterationData.get('qty');
const expected = Number(pm.iterationData.get('status'));
pm.test(`CART: quantity ${qty} gives ${expected}`, () => pm.response.to.have.status(expected)); - The test name includes the row, so the report says which boundary failed.line 3
Export, and run with Newman
The collections and environment in your folder, run from the command line with a JUnit report.
Export everything into your project folder: on each collection, ... then Export (Collection v2.1); on the environment, ... then Export. Use the file names below. The full exported files are shown here; yours will match apart from ids Postman adds.
Then add package.json and .gitignore, and install Newman, Postman's command-line runner. npm test runs the main collection with the environment, prints results in the terminal and writes results/newman.xml in JUnit format, which every CI server can show. npm run test:quantities runs the data-driven collection.
If any assertion fails, Newman exits with code 1, which fails a CI job. Part 8 puts this in GitHub Actions.
{
"name": "shoprung-postman",
"private": true,
"scripts": {
"test": "newman run shoprung.postman_collection.json -e local.postman_environment.json -r cli,junit --reporter-junit-export results/newman.xml",
"test:quantities": "newman run quantities.postman_collection.json -e local.postman_environment.json -d quantities.csv"
},
"devDependencies": {
"newman": "6.2.3"
}
} -r cli,junitprints to the terminal and writes the XML report.line 5-dgives the data file: one iteration per row.line 6- Pinned, like any test dependency.line 9
node_modules/
results/ {
"info": {
"name": "ShopRung API",
"description": "API checks for ShopRung, the practice shop. Run in order: each folder builds on the one before.",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
},
"item": [
{
"name": "0 Setup",
"item": [
{
"name": "Reset the shop",
"request": {
"method": "POST",
"header": [],
"url": "{{baseUrl}}/api/test/reset"
},
"event": [
{
"listen": "test",
"script": {
"type": "text/javascript",
"exec": [
"pm.test('reset: 200', () => pm.response.to.have.status(200));"
]
}
}
]
},
{
"name": "Log in as Alice",
"request": {
"method": "POST",
"header": [
{
"key": "Content-Type",
"value": "application/json"
}
],
"url": "{{baseUrl}}/api/auth/login",
"body": {
"mode": "raw",
"raw": "{\"email\": \"{{aliceEmail}}\", \"password\": \"{{alicePassword}}\"}"
}
},
"event": [
{
"listen": "test",
"script": {
"type": "text/javascript",
"exec": [
"pm.test('login: 200', () => pm.response.to.have.status(200));",
"pm.environment.set('token', pm.response.json().token);"
]
}
}
]
},
{
"name": "Log in as Bob",
"request": {
"method": "POST",
"header": [
{
"key": "Content-Type",
"value": "application/json"
}
],
"url": "{{baseUrl}}/api/auth/login",
"body": {
"mode": "raw",
"raw": "{\"email\": \"bob@shoprung.test\", \"password\": \"Bob12345!\"}"
}
},
"event": [
{
"listen": "test",
"script": {
"type": "text/javascript",
"exec": [
"pm.test('login: 200', () => pm.response.to.have.status(200));",
"pm.environment.set('bobToken', pm.response.json().token);"
]
}
}
]
}
]
},
{
"name": "1 Auth",
"item": [
{
"name": "Wrong password",
"request": {
"method": "POST",
"header": [
{
"key": "Content-Type",
"value": "application/json"
}
],
"url": "{{baseUrl}}/api/auth/login",
"body": {
"mode": "raw",
"raw": "{\"email\": \"{{aliceEmail}}\", \"password\": \"wrong\"}"
}
},
"event": [
{
"listen": "test",
"script": {
"type": "text/javascript",
"exec": [
"pm.test('ACC: wrong password is 401', () => pm.response.to.have.status(401));",
"pm.test('ACC: generic message', () => pm.expect(pm.response.json().message).to.eql('Email or password is wrong'));"
]
}
}
]
},
{
"name": "Cart without a token",
"request": {
"method": "GET",
"header": [],
"url": "{{baseUrl}}/api/cart"
},
"event": [
{
"listen": "test",
"script": {
"type": "text/javascript",
"exec": [
"pm.test('no token is 401 unauthorized', () => {",
" pm.response.to.have.status(401);",
" pm.expect(pm.response.json().error).to.eql('unauthorized');",
"});"
]
}
}
]
}
]
},
{
"name": "2 Cart",
"item": [
{
"name": "Add 4 notebooks ($50.00)",
"request": {
"method": "POST",
"header": [
{
"key": "Content-Type",
"value": "application/json"
},
{
"key": "Authorization",
"value": "Bearer {{token}}"
}
],
"url": "{{baseUrl}}/api/cart/items",
"body": {
"mode": "raw",
"raw": "{\"productId\": 1, \"quantity\": 4}"
}
},
"event": [
{
"listen": "test",
"script": {
"type": "text/javascript",
"exec": [
"pm.test('CART: 200', () => pm.response.to.have.status(200));",
"pm.test('subtotal is $50.00', () => pm.expect(pm.response.json().subtotalCents).to.eql(5000));"
]
}
}
]
},
{
"name": "Apply SAVE10 at $50.00",
"request": {
"method": "POST",
"header": [
{
"key": "Content-Type",
"value": "application/json"
},
{
"key": "Authorization",
"value": "Bearer {{token}}"
}
],
"url": "{{baseUrl}}/api/cart/coupon",
"body": {
"mode": "raw",
"raw": "{\"code\": \"save10\"}"
}
},
"event": [
{
"listen": "test",
"script": {
"type": "text/javascript",
"exec": [
"const cart = pm.response.json();",
"pm.test('PRICE: SAVE10 applies at exactly $50.00', () => pm.response.to.have.status(200));",
"pm.test('discount is $5.00', () => pm.expect(cart.discountCents).to.eql(500));",
"pm.test('total is $49.99', () => pm.expect(cart.total).to.eql('$49.99'));",
"pm.test('fast enough', () => pm.expect(pm.response.responseTime).to.be.below(1000));"
]
}
}
]
},
{
"name": "Expired WELCOME15",
"request": {
"method": "POST",
"header": [
{
"key": "Content-Type",
"value": "application/json"
},
{
"key": "Authorization",
"value": "Bearer {{token}}"
}
],
"url": "{{baseUrl}}/api/cart/coupon",
"body": {
"mode": "raw",
"raw": "{\"code\": \"WELCOME15\"}"
}
},
"event": [
{
"listen": "test",
"script": {
"type": "text/javascript",
"exec": [
"pm.test('PRICE: expired coupon is refused', () => {",
" pm.response.to.have.status(400);",
" pm.expect(pm.response.json().message).to.eql('This coupon has expired');",
"});"
]
}
}
]
}
]
},
{
"name": "3 Orders",
"item": [
{
"name": "Place the order",
"request": {
"method": "POST",
"header": [
{
"key": "Authorization",
"value": "Bearer {{token}}"
}
],
"url": "{{baseUrl}}/api/orders"
},
"event": [
{
"listen": "test",
"script": {
"type": "text/javascript",
"exec": [
"pm.test('ORD: 201 Created', () => pm.response.to.have.status(201));",
"pm.test('status is PLACED', () => pm.expect(pm.response.json().status).to.eql('PLACED'));",
"pm.environment.set('orderId', pm.response.json().id);"
]
}
}
]
},
{
"name": "Stock went down",
"request": {
"method": "GET",
"header": [],
"url": "{{baseUrl}}/api/products/1"
},
"event": [
{
"listen": "test",
"script": {
"type": "text/javascript",
"exec": [
"pm.test('ORD: stock of the notebook is 40 - 4 = 36', () => pm.expect(pm.response.json().stock).to.eql(36));"
]
}
}
]
},
{
"name": "Bob opens Alice's order",
"request": {
"method": "GET",
"header": [
{
"key": "Authorization",
"value": "Bearer {{bobToken}}"
}
],
"url": "{{baseUrl}}/api/orders/{{orderId}}"
},
"event": [
{
"listen": "test",
"script": {
"type": "text/javascript",
"exec": [
"pm.test('ORD: another customer gets 404', () => pm.response.to.have.status(404));"
]
}
}
]
},
{
"name": "Cancel a shipped order",
"request": {
"method": "POST",
"header": [
{
"key": "Authorization",
"value": "Bearer {{token}}"
}
],
"url": "{{baseUrl}}/api/orders/2/cancel"
},
"event": [
{
"listen": "test",
"script": {
"type": "text/javascript",
"exec": [
"pm.test('ORD: a SHIPPED order cannot be cancelled', () => {",
" pm.response.to.have.status(409);",
" pm.expect(pm.response.json().message).to.eql('Cannot cancel an order that is SHIPPED');",
"});"
]
}
}
]
},
{
"name": "Cancel the order",
"request": {
"method": "POST",
"header": [
{
"key": "Authorization",
"value": "Bearer {{token}}"
}
],
"url": "{{baseUrl}}/api/orders/{{orderId}}/cancel"
},
"event": [
{
"listen": "test",
"script": {
"type": "text/javascript",
"exec": [
"pm.test('ORD: a PLACED order can be cancelled', () => {",
" pm.response.to.have.status(200);",
" pm.expect(pm.response.json().status).to.eql('CANCELLED');",
"});"
]
}
}
]
}
]
}
]
} - Folders become
itemarrays, in run order.line 9 - Variables stay as variables in the export.line 196
{
"info": {
"name": "ShopRung quantity limits",
"description": "Run with -d quantities.csv: one iteration per row.",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
},
"item": [
{
"name": "Reset",
"request": {
"method": "POST",
"header": [],
"url": "{{baseUrl}}/api/test/reset"
}
},
{
"name": "Log in as Alice",
"request": {
"method": "POST",
"header": [
{
"key": "Content-Type",
"value": "application/json"
}
],
"url": "{{baseUrl}}/api/auth/login",
"body": {
"mode": "raw",
"raw": "{\"email\": \"{{aliceEmail}}\", \"password\": \"{{alicePassword}}\"}"
}
},
"event": [
{
"listen": "test",
"script": {
"type": "text/javascript",
"exec": [
"pm.environment.set('token', pm.response.json().token);"
]
}
}
]
},
{
"name": "Add quantity from the data file",
"request": {
"method": "POST",
"header": [
{
"key": "Content-Type",
"value": "application/json"
},
{
"key": "Authorization",
"value": "Bearer {{token}}"
}
],
"url": "{{baseUrl}}/api/cart/items",
"body": {
"mode": "raw",
"raw": "{\"productId\": 1, \"quantity\": {{qty}}}"
}
},
"event": [
{
"listen": "test",
"script": {
"type": "text/javascript",
"exec": [
"const qty = pm.iterationData.get('qty');",
"const expected = Number(pm.iterationData.get('status'));",
"pm.test(`CART: quantity ${qty} gives ${expected}`, () => pm.response.to.have.status(expected));"
]
}
}
]
}
]
} npm install
npm test
npm run test:quantities │ requests │ 13 │ 0 │ │ assertions │ 19 │ 0 │ │ total run duration: 338ms │ ... │ iterations │ 4 │ 0 │ │ assertions │ 4 │ 0 │
Stuck on this step?
Error: connect ECONNREFUSED- ShopRung is not running, or runs on another port. Override the variable for one run:
npm test -- --env-var baseUrl=http://localhost:PORT.
Check it works
Switch on ShopRung's mystery mode, which turns on five bugs without telling you which, and run both suites. The main collection fails six assertions in four places: SAVE10 at $50.00 (three checks), the stock, Bob seeing Alice's order, and cancelling a shipped order. The quantities run fails quantity 11 gives 400. Five bugs found. Newman exits with code 1 each time, so a CI job would go red.
Turn the bugs off at the end (-d '{"enabled":[]}').
curl -X POST http://localhost:8080/api/test/bugs -H "Content-Type: application/json" -d '{"enabled":["mystery"]}'
npm test
npm run test:quantities │ assertions │ 19 │ 6 │ # failure detail 1. AssertionError PRICE: SAVE10 applies at exactly $50.00 2. AssertionError discount is $5.00 3. AssertionError total is $49.99 4. AssertionError ORD: stock of the notebook is 40 - 4 = 36 5. AssertionError ORD: another customer gets 404 6. AssertionError ORD: a SHIPPED order cannot be cancelled ... 1. AssertionError CART: quantity 11 gives 400
Troubleshooting
Postman says "Could not send request" to localhost.
Use the desktop app. The web version needs the Postman agent to reach localhost.
Cart requests return 401.
The token is empty: run the Setup folder first, check the login script saves token, and that the request header is exactly Bearer {{token}} with the environment selected.
The second run fails where the first passed.
The reset request is missing or not first. Every run must start from the seed data.
{{qty}} is sent literally.
The data file was not selected in the Runner (or -d missing in Newman). Variables from data files exist only during a data-driven run.
Windows: the curl command for the bug switch fails.
Use curl.exe with -d "{\"enabled\":[\"mystery\"]}", or send the same request from Postman.
What changes in production
- Collections and environments live in Git and are reviewed like code; secrets come from CI variables (
--env-var), never from exported files. - Teams often explore and share requests in Postman, and keep their main regression suite in code (REST Assured or pytest), where reuse and review are easier.
- Newman runs in CI with JUnit or HTML reports; Postman also has its own CLI and a cloud runner for teams on paid plans.
- Collections double as living API documentation and as quick smoke tests after each deployment.
Interview angle
“How do you use Postman in test automation?”
I build collections with folders per feature and an environment per server, so nothing is hard-coded. The login request stores its token in a variable and later requests chain from it, including ids saved from earlier responses. pm.test checks status, exact body values, side effects and response time, named after the requirement. Data files drive boundary values. The exported collection lives in Git and runs in CI with Newman and a JUnit report, failing the build on any broken rule. For large suites I move to code-based tools.
Clean up
The full project
Every file from this lab, as it is after the last step.