Sphere
Log in Join
☆ 5

factory_boy or plain fixtures for test data, which do you actually use

Moving away from Django JSON fixtures to something more maintainable. Django 5.0, pytest-django 4.7. factory_boy seems powerful but adds a dependency and a bunch of concepts — SubFactory, traits, sequences, related factories. Is it worth it for a project with about 80 models, or is a set of helper functions enough? Looking for opinions from people who have genuinely used both for more than a week.

⚑ Report

4 replies

☆ 0
D dns ·2026-10-09#1

For 80 models, factory_boy pays off. The sequences alone — unique emails, slugs, usernames — save a ton of ad-hoc code. And SubFactory handles relations cleanly without you manually wiring foreign keys. The learning curve is about a weekend, and after that it's hard to go back.

⚑ Report
☆ 0
M moon ·2026-10-13#2

Do you define a factory for every model or only the ones that show up in tests? I don't want to maintain factories for thirty models nobody ever creates in a test. What's your cutoff rule? I'd rather not guess at a cutoff before we've actually written the tests.

⚑ Report
☆ 0
P proto ·2026-10-21#3

Only the ones you actually create in tests. We have factories for maybe 30 of our 80 models. Add more as needed. Start with a plain DjangoModelFactory and a Meta.model, don't build elaborate traits until a real test needs them.

⚑ Report
☆ 0
R rabbit ·2026-10-29#4

Counterpoint: we tried factory_boy and went back to simple helpers plus pytest fixtures because our tests were mostly reading seeded data rather than building complex object graphs. If your tests need five-level-deep relations, factory_boy wins decisively. If they're simple, helpers are less magic. Try it on one app first.

⚑ Report
Log in to reply.