QA Interview Hub · Knowledge base · Quizzes · Українська

System Design › Test data and environments

Test data strategies

The base rule: every test creates its own data and does not depend on other tests' data, and only unchanging reference data is shared. Approaches (API, test hook, seed, direct DB write) are chosen by isolation, speed and honesty of state: the API usually gives the best balance, while a direct DB write can create an impossible state. Data must be synthetic, and uniqueness comes from a UUID.

System design · Data and environments

The test is green on its own and red in the full run. Where does its data come from?

A transfer test passes when run alone and fails in the nightly run: the balance is «wrong». The test code did not change; its neighbours did, working with the same data. Test data is the most common cause of unstable and slow tests, and a Senior must explain where to get it and why that way. This lesson lays out the approaches by price and risk.

How it works and why. Data has three properties, and each approach gives up one for another. Isolation: my test does not see other people's changes. Speed: how much time goes on preparation. Honesty of state: the data is created the way the product itself creates it, so the state is possible in reality. A direct DB write is fastest but can create a state the application never would, for example an account with a balance and no entry in the operations journal. Creating through the UI is honest but slow and brittle. The API is in the middle: usually the best balance. So the table has no «best» approach, only a choice for the circumstances.

ApproachProsCons
Static data in the DBSimple, fastConflicts in parallel runs, data gets «spoiled»
Generating through the API in a testIsolation, realistic stateSlower, depends on the API
A test hook (internal endpoint)One request instead of KYC and verificationsMust be agreed with development and closed from production
A seed script or a DB snapshotFast environment startMaintained together with the schema, data goes stale
Factories writing straight to the DBVery fastBypasses business logic, an impossible state can be created

The basic rule: each test creates its own data and does not rely on the data of other tests. Only reference data that nobody changes may be shared: currencies, countries, tariffs. Note that «shared» and «unchangeable» go together. A shared write permission is exactly the source of conflicts.

Where a test gets its data

Typical

A shared ready-made user

All tests take test_user_1. While the test runs alone, all is fine. In a parallel run they change one balance.

Better

Own data per test

A fixture creates a user before the test and cleans up after. Neighbours cannot interfere.

Figure 1. The difference is who owns the state the test changes.

Example: from a shared user to an own one. It was: test_user_1 with a balance of 100 lies in the DB and dozens of tests use it. Now: a factory generates a unique user, and a fixture creates it through the API and cleans up after the test:

import { randomUUID } from 'node:crypto';

export const makeUser = (over: Partial<NewUser> = {}): NewUser => ({
  email: 'qa+' + randomUUID().slice(0, 8) + '@example.test',
  currency: 'EUR',
  ...over,
});

export const test = base.extend<{ user: User }>({
  user: async ({ api }, use) => {
    const user = await api.createUser(makeUser());
    await use(user);
    await api.deleteUser(user.id);
  },
});

Uniqueness comes from a random suffix (a UUID), not from the worker index: an index does not tell two consecutive runs apart and does not help on a retry. The example.test domain is reserved for tests and will not deliver mail anywhere.

Fintech. Creating a «ready» user often requires KYC, phone verification and opening an account. A test hook helps: an internal endpoint available only in test environments that creates a verified user in one request. Conditions: agree it with development, close it from production by configuration (not only by a «secret» path) and have a check that in a production-like configuration it is unavailable. The data must be synthetic: no real personal data and no real cards, only test cards described in the provider documentation. If a copy of production is needed, it is anonymised before it reaches the test environment.

Limits and alternatives. Generation through the API adds time to every test, so on large suites per-worker account pools appear (see the next lesson). A DB snapshot or seed brings a database up fast, but must be refreshed with migrations; in PostgreSQL a database can be created from a template (CREATE DATABASE … TEMPLATE), which is handy for a separate copy per worker. Static data is acceptable when a test only reads (say, a smoke check on reference data) or when runs go one at a time. A direct DB write is justified for volume (a million records to check pagination), provided the state has been reconciled with business rules at least once.

Common mistakes.

  • One «golden» user for all tests: works while there is no parallelism and breaks exactly when it is switched on.
  • Dependence between tests («test 2 uses what test 1 created»): order and parallelism break both.
  • A direct DB write without checking invariants: the test verifies a state that never occurs in reality.
  • Real personal data and real cards in tests: a risk of leaks and of violating regulatory requirements.
  • Not cleaning up: the database grows and queries get slower and less stable.

Check yourself

Why is it better to make an email unique with a UUID rather than with a worker index?

Answer

A worker index repeats between runs and after a rerun: data from the previous run may remain and cause a conflict. A UUID is always unique, regardless of how many workers there are and how many times the suite was run.

What is the advantage of creating state through the API over a direct DB write, and when is a direct insert still justified?

Answer

The API passes data through business logic, so the state is possible in reality (the balance has an operation entry, validation passed). A direct write gives speed but can create an impossible state. It fits a large volume of data where creating through the API is too slow, and only with invariants preserved.

In an interview. Junior: Each test creates the data it needs through the API or a test hook and does not depend on others. I keep only unchangeable reference data shared, and I do not use real personal data in tests. Middle/Senior: I choose the approach by three properties: isolation, speed and honesty of state. The API gives the best balance; a direct DB insert is faster but may create an impossible state, so I keep it for large volumes. I ensure uniqueness with a UUID, and I close a test hook from production by configuration and verify that. Production copies only after anonymisation.

More articles in this chapter