Short answer
Tests are repeatable when each test creates its own data and doesn’t depend on other tests’ data. In practice: data created via the API at the start of the test, unique identifiers (e.g. an email address with a random suffix), a few fixed role accounts loaded by a script when the environment is set up, and synthetic data instead of a production copy. A copy of the production database is usually a bad idea: it contains personal data covered by GDPR and changes with every refresh.
Four ways to get test data
| approach | when it fits | risk |
|---|---|---|
| data created in the test (API, database) | automated tests, most scenarios | a longer test if creating data is slow |
| fixed data loaded by a script | lookup tables, role accounts, catalogue products | tests that change this data break it for others |
| synthetic data in bulk | performance tests, reports, search | you need a realistic distribution |
| anonymised production copy | rare cases where the real data distribution matters | anonymisation must be complete, otherwise it’s still personal data |
For most teams the first two are enough.
Rules
- One test, its own data. A test doesn’t use an order or account created by another test.
- Unique identifiers.
[email protected]instead of[email protected], so parallel tests don’t collide. - Create via the API, not the UI. Creating an account via the API takes a fraction of a second, through a form it takes several seconds.
- Fixed accounts are read-only. An admin account for permission tests can be shared, as long as no test changes it.
- Relative dates. “An order from 30 days ago” instead of “an order from 1 October”, because next month the test will stop working.
- A test data prefix. For example
qa-in names, so the data is easy to find and remove.
Example in Playwright
A fixture that creates a user via the API before the test and deletes it afterwards:
import { test as base, expect } from "@playwright/test";
import { randomUUID } from "crypto";
type User = { id: string; email: string; password: string };
export const test = base.extend<{ user: User }>({
user: async ({ request }, use) => {
const email = `qa-${randomUUID().slice(0, 8)}@example.com`;
const password = "Password123!";
const res = await request.post("/api/users", { data: { email, password } });
expect(res.ok()).toBeTruthy();
const { id } = await res.json();
await use({ id, email, password });
await request.delete(`/api/users/${id}`);
},
});
test("new user sees an empty cart", async ({ page, user }) => {
// log in and check the cart using the data from the user fixture
});
Every test gets a fresh account, tests can run in parallel, and the environment doesn’t fill up with leftovers. Missing rules like these are one of the most common causes of flaky tests.
Personal data and GDPR
A copy of the production database in a test environment is still processing of personal data: you need a legal basis, access control and retention rules. On top of that, test environments are usually less well secured than production. It’s safer to:
- generate synthetic data, e.g. with the Faker library and a suitable locale;
- if a copy is unavoidable, anonymise it before loading it, not afterwards;
- not copy production data into cloud-based testing tools.
Data for manual testing
Manual testers need data too: accounts for every role, orders in different states, a customer with a long history, a customer with nothing. Write them down in one place and recreate them with a script after every environment refresh. Half a day of a tester’s time spent preparing data before each regression run is a cost that is easy to eliminate.
How I can help
As part of test automation I set up test data together with the tests: fixtures that create data via the API, scripts for fixed data and clean-up. For API testing I prepare data for the negative scenarios. Describe your app and test environment in the contact form.