Unique test data when API tests run in parallel
Your API tests pass alone and fail together. Two pull requests push at the same time, both pipelines run the collection against staging, and both try to create [email protected]. One wins. The other gets 409 Conflict, and someone reruns it, and it passes, and the team starts calling the tests flaky.
They're not flaky. They share data.
A run id
In the collection's pre-request script, create a run id once per iteration, if there isn't one yet:
if (!pm.variables.get('runId')) {
const uuid = require('uuid');
pm.variables.set('runId', uuid.v4().slice(0, 8));
}Script-set values carry to the later requests of the iteration, so every request sees the same runId. Then use it wherever you create data:
{
"email": "qa+{{runId}}@example.com",
"companyName": "Test Co {{runId}}",
"externalRef": "run-{{runId}}"
}In CI, you can pass something more meaningful, which also shows up in the API's own logs:
requestarium run-collection "Orders API" --env Staging --var runId="gh-$GITHUB_RUN_ID"--var wins over the script's check, because the variable already exists.
Several per run
With --iterations or a data file, each iteration starts fresh and gets its own runId from the script above. If you'd rather keep one id per run and number the iterations:
{ "email": "qa+{{runId}}-{{iteration}}@example.com" }pm.variables.set('iteration', pm.info.iteration);Find what you made, not what's there
On a shared environment, lists contain other runs' data. A test that reads items[0] and expects it to be the order it just created will fail at random. Save ids when you create things, and fetch by id:
// Tests on "Create order"
pm.environment.set('orderId', pm.response.json().id);When you must search a list, filter by your run's marker:
const mine = pm.response.json().items.filter((order) => order.externalRef === 'run-' + pm.variables.get('runId'));
pm.test('my order is in the list', () => {
pm.expect(mine).to.have.lengthOf(1);
});Clean up your own data only
A cleanup request that deletes "all test users" will delete the user another pipeline is in the middle of using. Delete what this run created, by id or by run marker. See setup and cleanup steps.
Data left behind by crashed runs piles up. A nightly job that deletes test data older than a day, matched by your marker (qa+ emails, run- refs), keeps staging tidy.
Things a run id doesn't fix
- Shared global settings. Two runs that both toggle the same feature flag still collide. Test those in a separate, serial pipeline.
- Rate limits. Parallel runs share the API's limits. If you see
429s only when pipelines overlap, that's why. - Unique constraints on fixed values. If a field must be one of a few values and unique (one "default" address per user), create a fresh parent per run.