Skip to content
TestLine Menu
TestLine
automation

Test data: how to prepare it so tests are repeatable

Four ways to get test data, when to choose each, an example in Playwright, and why a copy of production is usually a bad idea.

author
Artur Machowski, Senior QA Engineer
published
reading
6 min

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

approachwhen it fitsrisk
data created in the test (API, database)automated tests, most scenariosa longer test if creating data is slow
fixed data loaded by a scriptlookup tables, role accounts, catalogue productstests that change this data break it for others
synthetic data in bulkperformance tests, reports, searchyou need a realistic distribution
anonymised production copyrare cases where the real data distribution mattersanonymisation 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.

Frequently asked questions

How do you prepare test data for automated tests?

Ideally each test creates its own data via the API or directly in the database, with a unique identifier, and doesn't rely on other tests' data. Fixed data, such as lookup tables and role accounts, is best loaded by a script when the environment is set up.

Can you test on a copy of the production database?

Usually it's not worth it. A production copy contains personal data, so GDPR applies, and it is also large and not repeatable. If you need a realistic data distribution, use synthetic data or a properly anonymised copy.

What is synthetic data?

Data generated by code that looks real but belongs to nobody: names, addresses, account numbers in a valid format. Libraries such as Faker generate it in many languages and locales.

How do you clean up data after tests?

The simplest way is to delete the data a test created in its final step, or to periodically clear the environment of data with the test prefix. More important than clean-up is that no test depends on what other tests left behind.

Why does shared data break tests?

When two tests use the same account or order, one changes its state while the other expects the previous one. When tests run in parallel, the result depends on the order and the tests become flaky.

Got a release in a few weeks?

Describe your project in the form. I'll reply with questions about scope or an initial test plan.