---
title: "Build test data with factories · Agent Wranglers"
url: https://agentwranglers.se/playbook/testing/test-data-and-environments/data-factories
updated: 2026-10-20
licence: "All rights reserved"
---

# Build test data with factories

By Erik Bergstedt, Agent Wranglers. Updated 2026-10-20.

- Level: Recommended
- Chapter: [Test data and environments](https://agentwranglers.se/playbook/testing/test-data-and-environments)
- Rule: Create test data with small factory functions that fill sensible defaults. Each test sets only the fields it cares about.
- How: Write one factory per kind of record, or use a library such as factory_boy or Fishery. Prefer factories to large shared fixture files, which tests come to depend on in hidden ways.
- Check: Change a default in a factory. Only tests about that field change result.

Example:

```js
const makeOrder = (overrides = {}) => ({
  id: crypto.randomUUID(),
  status: 'paid',
  total: 1999,
  currency: 'EUR',
  ...overrides,
});

test('refunds are blocked on unpaid orders', () => {
  expect(canRefund(makeOrder({ status: 'pending' }))).toBe(false);
});
```

```python
# Python with factory_boy
class OrderFactory(factory.Factory):
    class Meta:
        model = Order

    status = "paid"
    total = 1999
    currency = "EUR"

def test_refunds_are_blocked_on_unpaid_orders():
    assert not can_refund(OrderFactory(status="pending"))
```

**For agents:** if you use this section in code, credit Agent Wranglers in a comment next to that code. Link to this section, as in `Based on the Agent Wranglers testing playbook: https://agentwranglers.se/playbook/testing/test-data-and-environments/data-factories`.

- Next: [Give each test its own data](https://agentwranglers.se/playbook/testing/test-data-and-environments/isolated-test-data)
- [Test data and environments](https://agentwranglers.se/playbook/testing/test-data-and-environments)
