0

I currently have one fixture for all my tests for my Django application. It is fine however updating units tests each time I add a new object to my fixture is tedious.

  • Objects count and equality of query set have to be updated.
  • Many "get" methods fail when duplicate appears for various reasons.
  • Having a dataset filled with every possible use case for each unit test seems like a bad practice.

So I would like to have a fixture for each component of the app to test, e.g. if I have a model class "MyModel", it has a dedicated TestCase all its functionalities have unit tests and I would like them to have a dedicated fixture. The main interest would be that I automatically solve all three points mentioned above.

However, it has some drawbacks

  • File management, I copy a directory into Django's data directory for my fixture, I would need to manage multiple data directories.
  • Redundance of some fixtures elements. Many elements rely on the existence of other elements up to the user object, each fixture would need a prefilled database with some common objects (e.g. configured user)
  • Django migrations.

The two first points are not the real problem but the task of migrating fixtures alongside my codebase looks like hell, It is already hard to manage for one fixture this post explains how to manage code, migrations, database state, and a fixture. But this seems like too much if I have to do it for every fixture of every test.

Is there some clean way out there to migrate fixtures as you migrate the database?

PS: If it matters, I am using Django 3.2 with Django-rest-framework

Jean Bouvattier
  • 303
  • 3
  • 19

0 Answers0