Skip to content
Tutorials Center
My learning

Topics

Lesson 25 of 30 ยท Projects and Practice

Project: test suite for a CRUD API

Build a complete test suite for a to-do CRUD API: environments, bearer auth, chained create-read-update-delete tests, negative cases and clean-up.

It is time to put the pieces together. In this project you will design a complete test suite for a small CRUD API: Create, Read, Update and Delete operations on a collection of to-do items. You will use almost everything from the earlier chapters: a collection organized in folders, an environment holding the base URL and token, authentication inherited from the collection, tests with assertions, requests chained through variables, negative cases and clean-up at the end.

The real Postman collection is a GUI artifact, so it appears here as a plan and as read-only JSON snippets. What you can run is a stand-in: a real little HTTP server that runs inside the Piston sandbox on a loopback port, and a hand-written mini test runner that sends real requests to it with fetch and checks the answers the way Postman scripts do. The server is the "system under test"; the runner plays the part of the Collection Runner.

The API under test

The to-do API has these routes. Every route needs an Authorization: Bearer demo-token header.

Method and path Purpose Success Typical failures
GET /todos List all to-dos 200 and an array 401 without a token
POST /todos Create one 201, a body and a Location header 422 if the title is missing, 400 for broken JSON
GET /todos/{id} Read one 200 404 if it does not exist
PUT /todos/{id} Replace one 200 404, 422
PATCH /todos/{id} Change some fields 200 404
DELETE /todos/{id} Remove one 204 and no body 404

Start by getting a feel for the API. The first stand-in starts the server and sends a few requests by hand, the exploratory step that always comes before writing tests.

Node.js Explore the to-do API by hand

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Exploring has taught you the behavior: creation answers 201 with a Location header, delete answers 204 with no body, and a deleted item then answers 404. Those observations become the assertions of your suite.

Plan the collection

A good collection mirrors how you think about the API. Create one collection named To-do API and organize it into folders that run in a sensible order:

Collection layout

00 Auth checks No token, wrong token.
10 CRUD flow Create, read, update, delete in order.
20 Negative cases Bad input, unknown ids.
99 Clean-up Remove anything the run created.

Numbered folders make the run order obvious.

1 / 2

Three decisions here pay off for the whole life of the suite:

  1. Put the base URL and token in an environment. You will have a local, a staging and perhaps a production-like environment. Switching is one click, and no request contains a hard-coded address. See Environments and variables.
  2. Set authentication once, on the collection. Choose Bearer Token with the value {{token}} and let every request inherit it. Only the requests that test missing credentials override it. See Authentication.
  3. Chain with variables, not copied ids. The create request saves the new id into a variable, and later requests use {{todo_id}}. Never type an id you saw once, because the next run will create a different one. See Chaining requests and workflows.

In Postman, the create request's Tests tab would hold this (read-only here, as it needs a real response):

JavaScript
pm.test("Created with 201 and a Location header", () => {
  pm.response.to.have.status(201);
  pm.response.to.have.header("Location");
});

const todo = pm.response.json();
pm.test("Body has an id and the title we sent", () => {
  pm.expect(todo.id).to.be.a("number");
  pm.expect(todo.title).to.eql("Write tests");
});

pm.collectionVariables.set("todo_id", todo.id);

The suite, step by step

The second stand-in is the whole suite. It starts the same server, then runs requests in the planned order. Notice four things as you read: the environment object, the variable todo_id that the create step saves and later steps use through {{todo_id}}, the negative cases, and the clean-up that leaves the server as it found it.

Node.js The full suite: auth, CRUD flow, negative cases, clean-up

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Read the output from top to bottom and match it to the plan. Every request announces its folder number, and every group of assertions is small and named after what it protects. When one fails, the name tells you what broke before you open anything.

Why clean-up and order matter

A suite that creates data and never removes it will eventually fail for the wrong reason: the list endpoint returns items from yesterday's run, a unique-name rule rejects a repeat, or a shared staging database fills with junk. Clean-up has two parts. Make each test independent of leftovers (for example, create a uniquely named item per run, using a timestamp or the built-in {{$guid}}), and delete what you created in a final folder. In Postman you can also put the delete in a collection-level post-response script, but a plain final folder is easier to read.

Order matters because the CRUD folder depends on the create step. If creation fails, the variable todo_id is never set, and every later request fails with a confusing 404. Postman lets you stop a run early with pm.execution.setNextRequest(null) when a prerequisite fails, so one real problem does not appear as ten.

Run it and keep it

In Postman you would open the Collection Runner, choose the collection and an environment, and press Run. The results list shows each request with its tests. To make this a safety net rather than a one-off, run the same collection from the command line with Newman, as in Newman, the CLI and CI/CD, and add it to your pipeline in Project: automated regression in CI.

The suite above uses a hand-written runner and a small Node server, not Postman or a real database. What transfers is the structure: environment, inherited auth, numbered folders, chained variables, positive and negative tests, and clean-up.

Ideas to extend it

  • Add a test that POST with a 10,000-character title is rejected (boundary testing).
  • Add a response-time assertion such as pm.expect(pm.response.responseTime).to.be.below(500).
  • Add a pagination test if the list endpoint ever supports ?page= and ?limit=.
  • Move the shared response checks (status, JSON content type) into a collection-level script.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Why should the create request save the new id in a variable instead of the next request using a fixed id?

    The server assigns the id, and it changes from run to run. Saving it in a variable makes the chain work every time.
  2. 2. Where is the best place to set the Bearer token for every request in the collection?

    Set auth once on the collection with a variable such as {{token}} and let requests inherit it. Only negative auth tests override it.
  3. 3. What is the purpose of the final clean-up folder?

    Leftover data makes later runs fail for unrelated reasons. Deleting what you created keeps the suite repeatable.
  4. 4. A delete request returns 204. Which assertion is appropriate?

    204 means success with no content, so the status is 204 and the body should be empty.
  5. 5. Why include negative cases such as a missing title or an unknown id?

    Checking that failures return the right status and message proves the API handles bad input safely.

Summary

  • A CRUD suite explores first, then turns each observed behavior into a named assertion.
  • Organize the collection into numbered folders: auth checks, the CRUD flow, negative cases and clean-up.
  • Keep the base URL and token in an environment, and set authentication once on the collection so requests inherit it.
  • Chain requests with variables such as todo_id, never with ids copied by hand.
  • Test the failures (401, 404, 422, 400) as carefully as the successes.
  • Clean up what a run creates, and stop early if a prerequisite step fails.
  • The runnable code is a stand-in: a Node server and a mini runner, not real Postman.

Next: Project: testing a login and token-refresh flow.