Keep API tests independent of each other

The login request fails, and the run report shows 47 red requests. Only one thing broke, but you can't see which from the report. Or a folder passes when you run the whole collection and fails when CI runs it alone. Both are signs of the same thing: tests that depend on each other without anyone deciding they should.

Find the hidden dependencies

Run each folder alone:

Terminal
for folder in Auth Orders Refunds Reports; do
  requestarium run-collection "Shop API" --folder "$folder" --env Staging --quiet || echo "✗ $folder fails alone"
done

A folder that passes in the full run and fails alone is borrowing something: a token, an id, a record created by an earlier folder. Usually the report shows an unresolved variable or a 401.

Give each folder its own setup

Put what a folder needs in the folder itself:

  • Auth: a pre-request script on the folder, or the collection, that logs in when there's no token yet. Every folder then works alone, and the token is fetched once per iteration.
  • Test data: a first request in the folder that creates what the rest use, and a last one that deletes it. See setup and cleanup steps.
  • Fixed reference data (a product that always exists on staging): an environment variable, not something an earlier folder looked up. See test data that differs per environment.

Chains are fine, inside a folder

Some tests are chains by nature: create an order, read it, refund it, check the refund. That's one scenario. Keep it in one folder, in order, short:

Output
Refunds/
  Create order
  Pay order
  Refund order
  Get order (status is refunded)

Run such folders with --bail in CI, or guard each step, so a failed first step doesn't produce three misleading failures:

JavaScript
// Pre-request on "Refund order"
if (!pm.environment.get('orderId')) {
  throw new Error('No orderId: "Create order" failed, skipping.');
}

The report then says why this step errored, pointing at the real failure.

No cross-folder order

The runner goes through folders in the order they appear in the tree. That's an implementation detail you shouldn't lean on: someone drags a folder, or CI runs one folder alone, and the order changes. If folder B needs folder A first, B should do A's setup itself.

One assertion failing shouldn't stop others

Inside a request, write several small tests instead of one big one. A failing pm.test doesn't stop the next pm.test in the same script, so the report shows everything that's wrong with the response, not only the first thing.

Independence makes everything else possible

Independent folders can run one at a time in CI, in parallel against different environments, and in suites that mix folders from several collections. Dependent ones can only ever run as one long chain.