QA AND TEST AUTOMATION · PART 4 · API TESTING · TOOL · 40 OF 112

Postman API Testing and Newman in CI, Step by Step

API testing with Postman, then running the same collection anywhere with Newman: collections and folders, environments and variables, saving a login token in a test script, pm.test assertions, data-driven runs from a CSV of boundary values, and Newman in CI with JUnit reports. Run for real against ShopRung: 4 iterations, 12 requests, 8 assertions passed.

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
  46. 46
  47. 47
  48. 48
  49. 49
  50. 50
  51. 51
  52. 52
  53. 53
  54. 54
  55. 55
  56. 56
  57. 57
  58. 58
  59. 59
  60. 60
  61. 61
  62. 67
  63. 68
  64. 69
  65. 70
  66. 71
  67. 72
  68. 73
  69. 74
  70. 75
  71. 76
  72. 77
  73. 78
  74. 79
  75. 80
  76. 81
  77. 82
  78. 83
  79. 84
  80. 85
  81. 86
  82. 87
  83. 88
  84. 89
  85. 90
  86. 91
  87. 92
  88. 93
  89. 94
  90. 95
  91. 96
  92. 97
  93. 98
  94. 99
  95. 100
  96. 101
  97. 102
  98. 103
  99. 104
  100. 105
  101. 106
  102. 107
  103. 108
  104. 109
  105. 110
  106. 111
  107. 112
1 / 12

Press play to hear the narration, or step through at your own pace.

Key takeaways

  • Collections in Git
  • environments and variables
  • token chaining
  • pm.test
  • CSV data
  • Newman in CI with reports

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 collection
  • 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.

Environments
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.

Login: Tests tab
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.

Common checks
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.

quantities.csv + test
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.

A real run
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.

Newman in a pipeline
- 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 vs code
  • 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.

Avoid these
  • 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.

In the interview
  • "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."

Interview questions

Q1What is Newman?

Postman's command-line collection runner; it runs exported collections with environments and data files, produces reports (CLI, JUnit, HTML via plugins) and fits into CI.

Q2How do you pass a token from login to other requests in Postman?

In the login request's test script, read the token from the response and store it with pm.environment.set("token", ...); other requests use {{token}} in their Authorization header.

Q3What is the difference between environment and collection variables?

Environment variables change per target (local, staging) and switch together; collection variables belong to the collection and stay the same across environments.

Q4How do you run data-driven tests in Postman?

Use a CSV or JSON data file with the collection runner or newman run -d; each row is an iteration and its values are available as variables.

Q5How do you assert a response in Postman?

With pm.test and pm.expect or pm.response.to.have..., checking status, body fields, headers and response time.

Q6How do you run Postman tests in CI?

Export the collection and environment to Git and run newman run collection.json -e env.json with a JUnit reporter; failed assertions make the job fail.

Q7What are the limits of Postman for large suites?

Logic scattered across scripts, hard reviews of JSON exports, limited reuse and refactoring; code-based frameworks scale better.

Q8What are pre-request scripts used for?

Preparing data before a request: generating unique values, computing signatures or timestamps, or fetching a token.