Module 3. Practice: from requirements to automated tests · Lesson 23 of 41 · 17 min

Postman and APIs in practice: collections, environments, tests and chains

From a single request to a collection with automatic checks: environment variables, the token, passing ids between requests, running from the command line.

From a request to a collection

One request in Postman is a manual check. A collection with tests and variables is already an API regression suite you can run every day.

Postman: request, environment variables and testsPostman · Collection “Shop API” · Environment: stagingPOST{{baseUrl}}/api/ordersSendParams · Authorization (Bearer {{token}}) · Headers · Body · Testspm.test("status 201", () => pm.response.to.have.status(201));pm.test("has id", () => pm.expect(pm.response.json().id).to.be.a("number"));pm.environment.set("orderId", pm.response.json().id);201 Created · 214 ms · 412 B✓ status 201✓ has id{ "id": 9051, "status": "new", "total": 125.00 }
Postman: request, environment variables and tests. A schematic snapshot. The base URL and token come from environment variables, and the Tests tab checks the status, schema and response time.

Environment variables

Create staging and dev environments with the same variable names:

Variable staging dev
baseUrl https://staging.shop.example http://localhost:3000
userEmail qa+promo@example.com qa@local.test
token (set automatically) (set automatically)

Write {{baseUrl}}/api/orders in your requests and switch environments with one click.

Getting the token automatically

In the login request, the Tests tab (or Post-response script):

pm.test("login succeeds", () => pm.response.to.have.status(200));
pm.environment.set("token", pm.response.json().accessToken);

At the collection level, under Authorization, choose Bearer Token with the value {{token}} — every request gets it automatically.

Tests for our feature

// POST {{baseUrl}}/api/cart/{{cartId}}/promo   body: { "code": "SALE10" }
pm.test("status 200", () => pm.response.to.have.status(200));
pm.test("10% discount", () => {
  const b = pm.response.json();
  pm.expect(b.discount).to.eql(12.5);
  pm.expect(b.total).to.eql(112.5);
});
pm.test("responds in under 800 ms", () => pm.expect(pm.response.responseTime).to.be.below(800));

Negative requests in the same collection: a nonexistent code (expect 404 or 422 with an error message), an expired code, an empty one, a lowercase code, applying the same code twice.

Chaining requests

  1. Log in → save the token.
  2. Create a cart → save cartId.
  3. Add an item → check the total.
  4. Apply the promo code → check the discount.
  5. Place the order → save orderId.

Each step takes the previous step's data through pm.environment.set and {{variable}}. That's how a collection becomes an end-to-end scenario.

Running without the UI

npx newman run shop-api.postman_collection.json \
  -e staging.postman_environment.json \
  --reporters cli,junit --reporter-junit-export results.xml

A run like this is easy to add to CI — see CI/CD for automated tests.

Hygiene rules

  • Don't store real passwords and tokens in exported collections — use secret-type variables.
  • Each test checks one thing and has a clear name.
  • Test data is created by requests in the collection, not by hand in the database.

HTTP and status code theory is in web and API testing and API testing in depth.

Key takeaways

  • Environment variables let you run the same collection against different environments.
  • The token and ids pass between requests automatically — that's how you build a scenario.
  • Newman runs a collection in CI without the UI.

Check yourself

  1. 1. Why save the token to an environment variable after login?

  2. 2. What does pm.expect(pm.response.responseTime).to.be.below(800) check?

  3. 3. How do you run a Postman collection in CI?

Practice: API testing in depth