As I'm developing unit tests for my django application I find myself running manage.py test over and over and over again. I'm using a MySQL backend and have many models in the project, so the ramp up time to create all of the test databases is about 30 seconds.
How can I use make each unit test faster by keeping the database tables created and just clearing them of all records in-between runs of the "manage.py test" command?
How To Add Unit Testing to Your Django Project Step 1 — Adding a Test Suite to Your Django Application. A test suite in Django is a collection of all the test cases in... Step 2 — Testing Your Python Code. In this step, you will test the logic of the code written in the models.py file. In... Step 3 ...
Running the tests requires a Django settings module that defines the databases to use. To help you get started, Django provides and uses a sample settings module that uses the SQLite database. See Using another settings module to learn how to use a different settings module to run the tests with a different database. Having problems?
In order to guarantee that all TestCasecode starts with a clean database, the Django test runner reorders tests in the following way: All TestCasesubclasses are run first. Then, all other Django-based tests (test cases based on SimpleTestCase, including TransactionTestCase) are run with no particular ordering guaranteed nor enforced among them.
A test suite in Django is a collection of all the test cases in all the apps in your project. To make it possible for the Django testing utility to discover the test cases you have, you write the test cases in scripts whose names begin with test.
Note that you don't have to run the entire test suite every time. You can just run the test suite for a single app by running manage.py test appname
(or for multiple apps at once with manage.py test app1 app2 ...
).
My usual workflow is to just run the tests for the app I'm working on as I work, and the run the full suite before I commit my next set of changes.
I haven't tried it, but there was a recent check in that is supposed to help by allowing the tests to be run inside transactions that are then rolled back:
Documentation and developer commentary
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