Test data that differs per environment

A request says GET /products/42. It passes on staging, where product 42 exists, and fails on your machine, where your database has products 1 to 10. Or it passes for months and fails the day someone cleans staging. Hard-coded test data ids are environment-specific values hiding in requests.

Move ids out of requests

Instead ofUse
GET /products/42GET /products/{{testProductId}}
"customerId": "cus_9a7f""customerId": "{{testCustomerId}}"
?warehouse=berlin-2?warehouse={{testWarehouse}}

Then fill in each environment:

VariableLocalStaging
testProductId342
testCustomerIdcus_local_1cus_9a7f
testWarehousedevberlin-2

An environment group can list the keys its environments are expected to have, which makes a missing one easy to spot. See environment groups.

Check it still exists

Staging gets cleaned, reset and migrated. Add a Preflight request at the start of the run that checks the fixed data is there:

JavaScript
// Tests on "Preflight: test data exists"
pm.test('the test product exists in this environment', () => {
  pm.response.to.have.status(200);
});

Run with --bail, and a missing product stops the run with one clear failure instead of thirty confusing ones.

Seed Local

Locally, the database may be fresh. A Seed folder creates the fixed data and saves the ids for the run:

JavaScript
// Tests on "Seed: create test product"
pm.environment.set('testProductId', pm.response.json().id);

Script-set values only last for the run, so run Seed together with the folder that needs it, or save the ids once with requestarium env set Local testProductId 3 after seeding. See keep a value from one run to the next.

Fixed or created per run?

Fixed test data, kept in the environment, is right for things that are read but never changed: a product catalogue item, a country, a currency. Anything a test changes should be created fresh by the run, or parallel runs will step on each other. See unique test data when tests run in parallel.