bkramer
bkramer

Reputation: 49

Why can't I make my own test database for my django project?

I am currently deciding what testing packages to use with my django project, specifically how to populate data before I run tests. I have looked into:

  1. Fixtures: Many people seem to be against this since modifying JSON can be difficult and hard to maintain.

  2. Factory boy: My project has a lot of hierarchy to it, so I feel like creating an object from a model that is really low in the hierarchy is really slow.

The idea I have in my mind is to just make a database that I will always run tests against. Since I know what data is in it, shouldn't it work similarly to fixtures without the hassle of JSON? Since I haven't seen this idea through my research, I assume this is a bad idea. Why is it a bad idea though?

Upvotes: 1

Views: 449

Answers (1)

RemcoGerlich
RemcoGerlich

Reputation: 31260

Did you actually test with factory boy? With an in-memory sqlite database it is very fast, in my experience, and super convenient with hierarchies.

Your database idea is more or less the same as fixtures -- you could use the database to create JSON fixtures by running the dumpdata management command on it, and maintain your test data in the database instead of in the JSON files.

Some reasons to prefer factory boy over a test database:

  • The data is generated right there in the test, so it's immediately obvious what happens and which fields are relevant for the test.

  • The data comes with the test code, no extra files or database dumps to manage.

  • Factory boy is very good with hierarchies ("I want an instance of this instance with all fields the default value, except this one field six steps up in the hierarchy" -- instance = InstanceFactory.build(series__study__patient__archive__project__algorithm_type='foo'))

  • Sometimes an object doesn't have to be saved to the database at all to have its methods tested, in that case factory boy's .build() is very fast.

  • Instead of picking interesting border cases for the current test, you will be tempted to just re-use the stuff you already have in the database and not find bugs.

  • Maybe the different values you need to test with can't exist in the database at the same time due to uniqueness constraints.

There are probably more, but I need to sleep.

Upvotes: 3

Related Questions