Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I run django unit tests (through manage.py) faster

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?

like image 858
MikeN Avatar asked Mar 05 '09 20:03

MikeN


People also ask

How to add unit testing to your Django project?

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 ...

How do I run tests with a different database in Django?

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?

How does the Django test runner reorder tests?

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.

What is a test suite in Django?

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.


2 Answers

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.

like image 199
jacobian Avatar answered Oct 07 '22 11:10

jacobian


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

like image 39
gerdemb Avatar answered Oct 07 '22 12:10

gerdemb