Skip to content

Test Data Factory

In one line

A test data factory creates test objects with default values — tests override only what matters, keeping test data DRY and maintainable.

In simple words

A test data factory is a function that creates test objects with sensible default values. Tests only specify the fields they care about — the factory fills in the rest with defaults.

Problem without factories:

  • Each test creates its own user/order/product with all fields
  • Lots of duplication — same boilerplate in every test
  • Hard to maintain — if the User model adds a required field, every test breaks

Solution with factories: const user = createUser({ name: 'Ada' });

  • Only specify what matters for this test (name)
  • Factory fills in defaults for everything else (id, email, createdAt, etc.)
  • Easy to maintain — add a default in one place

Factory features:

  1. Default values — sensible defaults for all fields
  2. Overrides — pass specific values for test-relevant fields
  3. Sequences — auto-increment IDs, unique emails
  4. Associations — automatically create related objects (user with posts)
  5. Traits — predefined variations (adminUser, guestUser)

Tools: Factory Boy (Python), @faker-js/faker (JS), fishery (JS/Ruby), FactoryBot (Ruby).

Example

javascript
import { faker } from '@faker-js/faker';

// Test data factory for User
function createUser(overrides = {}) {
  return {
    id: faker.number.int(),
    name: faker.person.fullName(),
    email: faker.internet.email(),
    role: 'user',
    active: true,
    createdAt: faker.date.recent(),
    ...overrides, // test overrides specific fields
  };
}

// Test: only specify what matters
test('admin can delete posts', () => {
  const admin = createUser({ role: 'admin' });
  expect(canDeletePosts(admin)).toBe(true);
});

test('regular user cannot delete posts', () => {
  const user = createUser(); // default role: 'user'
  expect(canDeletePosts(user)).toBe(false);
});

test('inactive user cannot login', () => {
  const user = createUser({ active: false });
  expect(canLogin(user)).toBe(false);
});

// Factory with associations
function createOrder(overrides = {}) {
  return {
    id: faker.number.int(),
    user: createUser(), // automatically create a user
    total: faker.number.float({ min: 0, max: 1000 }),
    status: 'pending',
    ...overrides,
  };
}

The factory creates objects with sensible defaults. Tests override only the fields they care about. This keeps test data DRY, maintainable, and focused on what each test is actually testing.

Common confusions

  • Confused with: test fixture

    The difference: A test data factory generates objects with defaults and overrides — it's dynamic and flexible. A test fixture is a fixed, known state — it's static and specific. Factories create data on demand; fixtures provide pre-defined state. Factories are better for complex/variable data; fixtures are simpler for fixed setups.

Related terms

Related Resources

Related Lessons

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors

← Back to glossary