Question

I am trying to do Test Driven Development with Django. My problem is that I need to add some things in the database before running the tests, for instance a number of users, departments and their permissions. I tried to do that using fixtures but I really find the whole process very unintuitive (also I found out that when I deleted and re-synced my database, my users fixtures were broken because the permissions are referenced only through the id)!

I have searched for other solutions to this pre-test database initialization, but I was able to only find very simple cases in which the initialization is performed in the setUp method of the TestCase class. I don't really thing that such methods could be used when you have an application with different users and permissions that all need to be tested.

Can you tell me how you fill your database before starting your tests for a big application ? Is there any better methods other than fixtures and setUp class ?

Was it helpful?

Solution

Model factories (and tools like factory_boy or model_mommy) is a better alternative.

According to Carl Mayer's slides on "Testing and Django", fixtures are:

  • Hard to maintain and update.
  • Increase test interdependence.
  • Slow to load.

It's better to use model factories, why?

  • Test data local to test code (explicit).
  • Easy to maintain.
  • Don't create any data you don't need for that test.
  • Works great even for large/complex test data sets (helper functions).

Personally, I've used factory_boy for testing in almost every django project. It's worth trying, take a look.

Also, see:

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top