Skip to content
Velloreth
Programming 6 min read

Test Data Management: Fixtures, Factories and Fakes

Test data is the hidden cost of a test suite. Learn factories, fixtures, seeding, and how to keep tests independent and fast.

By

The problem

Test data is the hidden cost of a test suite.

A shared fixture is fast to write and a common cause of a test that fails in isolation.

A large fixture slows every test that uses it.

A real personal data copy is a privacy and a compliance problem.

The goal: each test creates the data it needs, with the minimum setup, and no test depends on another.

Fixtures

A fixture is a fixed set of data a test uses.

const users = [
  { id: 1, name: 'Alice', role: 'admin' },
  { id: 2, name: 'Bob', role: 'user' },
];

The advantage: it is quick to write, and the data is visible.

The disadvantage: a test that depends on a specific row fails when the fixture changes, and the coupling is invisible.

Use a fixture for read-only reference data, such as a list of countries. Do not use it as the shared state of a suite.

Factories

A factory builds an object with sensible defaults.

function buildUser(overrides = {}) {
  return {
    id: 1,
    name: 'Test User',
    email: 'test@example.com',
    role: 'user',
    ...overrides,
  };
}

The test overrides only the fields it cares about:

const admin = buildUser({ role: 'admin' });

The advantage: the test states its intent, and a new required field is added in one place.

This is the pattern to prefer. It keeps the test focused and the setup minimal.

Builders

A builder is a fluent API for an object with many optional fields.

const order = new OrderBuilder()
  .withUser(1)
  .withItem({ price: 100 })
  .withCoupon('SAVE10')
  .build();

The advantage: it reads well for a complex object.

The disadvantage: it is more code than a factory, and it can hide the defaults.

Use a builder when the object has many optional fields and the test needs to set several of them.

Seeding a database

Seed only the data the test needs.

beforeEach(async () => {
  await db.query('TRUNCATE users, orders RESTART IDENTITY CASCADE');
  await db.users.create({ name: 'Alice' });
});

A large seed slows every test. A seed of a hundred rows for a test that reads one is wasted work.

Reset the data between tests, so a test does not see another’s data.

The fastest reset is a transaction rollback:

beforeEach(async () => { await db.query('BEGIN'); });
afterEach(async () => { await db.query('ROLLBACK'); });

The caveat: a test that commits, or that uses a separate connection, is not rolled back.

Seeding through the API

For an end-to-end test, seed through the API rather than the UI.

test.beforeEach(async ({ request }) => {
  await request.post('/api/test/reset');
  await request.post('/api/test/seed', { data: { user: 'alice' } });
});

It is faster than driving the UI, and it keeps the test focused on the journey it is testing.

Guard the endpoint. A test-only endpoint must not be reachable in production. Gate it on an environment variable, and return a 404 otherwise.

Independence

A test must create its own data.

// Bad: depends on the previous test
let userId;

test('creates a user', async () => {
  userId = await createUser('Alice');
});

test('finds the user', async () => {
  expect(await findUser(userId)).toBeDefined();  // fails alone
});

The fix:

test('finds the user', async () => {
  const userId = await createUser('Alice');
  expect(await findUser(userId)).toBeDefined();
});

A test that passes in the suite and fails alone is a test with a hidden dependency, and it is the most common cause of a flaky failure.

Privacy

Never use production data in a test.

It may contain personal data, which is a privacy and a compliance problem.

A test that writes to it is a data-loss risk.

Use synthetic data. A factory with a realistic shape is enough.

If you must use a production-like dataset, anonymise it first, and treat the anonymised copy as sensitive.

Determinism

Avoid a random value unless the test is about randomness.

// Non-deterministic: the test may pass or fail
const id = Math.random();

// Deterministic
const id = 42;

Avoid the current time unless the test is about time. Inject the clock.

Avoid a real network call. Stub it, or record the response.

A deterministic test fails for one reason, which is the point.

Cleaning up

Reset the state after the test, so the next test starts clean.

A test that leaves data behind affects the next test, which produces a failure that is hard to attribute.

Use a transaction rollback where possible, and an explicit cleanup otherwise.

Delete the created files in a test that writes to the disk.

Test data in a team

Test data is a shared resource, and the conventions around it matter as much as the code. A factory that everyone uses keeps the tests consistent, and a shared fixture that everyone edits becomes a source of conflict. The practice that scales is to keep the factories in one place, to make the defaults sensible, and to require a test to override only the fields it cares about. The second practice is to keep the test data out of the production database entirely, because a test that writes to production is a data-loss incident waiting to happen, and a copy of real personal data in a fixture is a privacy problem. The third is to make the data deterministic, so a test fails for one reason rather than intermittently, which is the difference between a suite the team trusts and one it ignores.

Practical guidance

  • Create the data the test needs.
  • Prefer a factory to a shared fixture.
  • Override only the fields the test cares about.
  • Seed only what the test needs.
  • Reset the data between tests.
  • Roll back a transaction where possible.
  • Seed through the API for an end-to-end test.
  • Never use production data.
  • Keep the data deterministic.

For the related guides, see unit-testing-basics and integration-vs-unit-tests. For the broader context, why-your-tests-are-slow and mocking-and-stubbing.

Quotable summary

A test should create the data it needs, so it does not depend on another test, because a test that passes in the suite and fails alone is a test with a hidden dependency. A factory builds an object with sensible defaults and the test overrides only the fields it cares about, which keeps the test focused and the setup minimal. A shared fixture is fast to write and a common cause of a test that fails in isolation, and a large fixture slows every test that uses it, so seed only the data the test needs. Never point a test at production data, because it may contain personal data and a test that writes to it is a data-loss risk, and keep the data deterministic so a test fails for one reason.

Frequently asked questions

What is a test fixture?
A fixed set of data a test uses. It can be a file, a database seed, or an object built in the test setup.
What is a test factory?
A function that builds a test object with sensible defaults, so the test overrides only the fields it cares about.
Why is a shared fixture a problem?
A test that depends on a shared fixture fails when run alone, and a change to the fixture breaks several tests at once.
Should I use a real database for test data?
Yes for an integration test, because the point is to test the real interaction. Use a container, and reset the data between tests.
Can I use production data in a test?
No. It may contain personal data, and a test that writes to it is a data-loss risk. Use synthetic data.
What is a builder pattern in tests?
A fluent API that constructs a test object step by step, which reads well for an object with many optional fields.