Im looking to learn more about your testing flows with Django.
Background information http://docs.djangoproject.com/en/dev/topics/testing/
Im encountering difficulties when using test driven development. The test runner of Django constantly creates all db models in a test db when starting. For our current projects (between 40 and 240 models) this means it takes easily 20s for tests to start.
This makes it completely unworkable for testing a new feature often. My question, how do you guys work around this?
I've tried a few things in the past
a.) - change the testloader to reuse the same test db every time and apply migrations when needed
b.) - run my unit tests from within the __main__
flow of python files
option b is awkward with the sys.path, option a is doable but doesnt seem to be the django way.
Update: Option A is indeed not such a bad solution. Its just quite a bit of effort. Which makes me believe people use a different workaround. SQL lite could be that workaround. But im guessing there are more.
change the testloader to reuse the same test db every time and apply migrations when needed
I don't see anything wrong in writing your own test runner that merely truncates the tables instead of dropping and creating the database. This is djangoic in that it solves a specific problem. There is a ticket open for allowing grouping of test cases into test suites. Once it is fixed you should be able to group your test cases into suites for easier management. You can also inspect the patch attached to the ticket and see if it will suit your purpose.
As Ned suggested you can use an in memory database. This depends to a large extent on your data model and queries being portable across databases.
If you haven't already try to reorganize your test cases. In my experience not all test classes need to sub class django.test.TestCase
. Find out those test classes that can do with sub classing unittest.TestCase
. This will speed up things a little bit.
Reorganize fixtures. Move common fixtures to a single file and load it before the test run rather than inside each test class (using fixtures = [...]
).
Using an in-memory SQLite database during testing definitely speeds things up.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With