Test data and environments Recommended
Build test data with factories
Create test data with small factory functions that fill sensible defaults. Each test sets only the fields it cares about.
How to do it
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.
Example
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 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"))
How to check
Change a default in a factory. Only tests about that field change result.
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.