On Fixtures and Factories

We’ve made it a general rule to move away from relying on fixtures in our projects. The main reasons are:

  • Fixtures are fragile. They often break when the schema changes or even worse they appear to work but introduce subtle bugs.
  • Extra work is sometimes needed in order to make fixtures portable (for example defining natural keys).
  • Processing large fixtures can be very slow, which slows down installation and testing cycles.
  • Other smart people in the community are recommending the same approach.

We look for good tools that are usually classified as either data generation or fixture factory. We’ve had some success with django-whatever and wanted to share a few tips. Some of the benefits of django-whatever:

  • Generating one or many instances of a Model can be done in a line or two of code.
  • Easy to handle things like non-standard primary keys or recursive relationships
  • Using a random generator, you get fuzz testing for free.

Like all powerful tools, it is easy to accidentally inflict pain. A few lessons we’ve learned:

  • If you have tests that fail randomly, it may be caused by a field on the model receiving random values whereas the tests are expecting a constant value.
  • Use any_user when creating User instances.

Finally, we’ve been discussing lately the merits of using something more declarative like factory-boy or perhaps finding another way to generate a full range of fuzz values without being so coupled to the unit tests. Lately we’ve found that patterns of similarity emerge between the parts of the unit test code and the data generation code. Once you have a solid base, it becomes frequently helpful to be able to use code to quickly generate real-world scenarios with complex relationships for testing and feedback.

What are your ideas about managing complex fixture data generation?

Brian Luft

About the author

Brian Luft

A Full-Stack developer by virtue of 15 years working with various types of companies and technologies; which is another way of saying he has no special skills. With a little sister unwilling to play D&D, he …

View Brian's profile

Recent Insights

Resetting Your South Migrations

Why? A common question from South users is how to reset the migrations back to an …

Disabling South Migrations

It is often handy to disable (either temporarily or permanently) South migrations for an app. “Disable” …

Why Regular Demos are Part of Our Development Process

Our team functions in different capacities for each business engagement, but we typically have a project …