Setup and clean-up steps for API test data

Most API tests need something to exist first. You can't test "pay an order" without an order, or "list my invoices" without a user who has invoices. And a test that creates data should remove it, or staging fills up with test-user-4821 until someone deletes it by hand.

Requestarium test suites have two roles for this: Setup and Clean up.

The roles

Open a suite in Tests, select a card on the canvas, and pick its Role in the Edit panel:

RoleRunsIf it fails
Setupfirst, in the Setup lane at the start of the canvasthe tests after it are not run, and the run counts as stopped
Test (default)in suite orderthe run continues, unless stop-on-failure is on
Clean uplast, in its own lanealways runs anyway

Skipping tests after a failed setup is deliberate. If the login failed, the results of the twenty requests that need a token would only say "401" twenty times. One clear failure in Setup is easier to read.

A typical suite

Output
Setup      POST  Log in as test user          → access_token
Setup      POST  Create customer              → customerId
Test       POST  Create order                 → orderId
Test       POST  Pay order
Test       GET   Get invoice
Clean up   DELETE Delete customer

Each request is a saved request from your collections. The suite points at it rather than copying it, so the request keeps its own auth, scripts and checks, and edits to it show up in every suite that uses it.

Passing values between steps

Select Log in as test user, open Save to variable, and add:

NameFrom
access_token$.access_token

Every step after it can use {{access_token}}, for example in an Authorization: Bearer {{access_token}} header. A value can also come from a header, such as Location after a create.

After a run, the fields of the response are offered as one-click suggestions, so you rarely type the path yourself.

Variables saved this way show under the card on the canvas as → access_token, so you can see at a glance which step produces what.

Scripts still work too. A post-response script with pm.environment.set('customerId', pm.response.json().id) passes the value to later steps in the same way.

When a value is missing

If Save to variable can't find its path (the login answered 200 but with an error body, say), the step fails. In Setup, that stops the suite. This is what you want: a missing token is the real problem, and it's reported where it happened.

Clean up that actually runs

Delete customer uses {{customerId}} from Setup. Because it's a Clean up step, it runs even if Pay order failed, so a broken build doesn't leave customers behind.

Write clean-up requests so they don't fail when there's nothing to clean: if Setup failed before creating the customer, {{customerId}} is empty and the delete gets a 404. Accept that in the request's checks:

JavaScript
pm.test('deleted, or nothing to delete', () => pm.expect(pm.response.code).to.be.oneOf([200, 204, 404]));

Setup per folder of tests

A package (a group of requests on the canvas) can have a role too. A Setup package with three requests (log in, create a project, invite a member) reads as one step, and opens to show its requests when you need them.

The same in the CLI and CI

Roles are part of the suite, so they apply wherever it runs:

Terminal
requestarium run --suite "Orders/Checkout" --env Staging --save-run --junit results.xml