For some time, my unit testing lasted longer than expected. I tried to debug it several times without much success, since there were delays before my tests even started to work. This affected my ability to do anything remotely close to testing-based development (maybe my expectations are too high), so I want to see if I can fix this once and for all.
When starting a test, there is a delay of 70 to 80 seconds between the start and actual start of the test. For example, if I run a test for a small module (using time python manage.py test myapp ), I get
<... bunch of unimportant print messages I print from my settings> Creating test database for alias 'default'... ...... ---------------------------------------------------------------- Ran 6 tests in 2.161s OK Destroying test database for alias 'default'... real 1m21.612s user 1m17.170s sys 0m1.400s
About 1m18 of 1m: 21 are between
Creating test database for alias 'default'...
and
.......
lines. In other words, the test takes less than 3 seconds, but the initialization of the database seems to take 1: 18min
I have about 30 applications, most of which have 1 to 3 database models, so this should give an idea of โโthe size of the project. I use SQLite for unit testing and have implemented some of the proposed improvements. I canโt publish the entire settings file, but I am happy to add any information that is required.
I use a runner
from django.test.runner import DiscoverRunner from django.conf import settings class ExcludeAppsTestSuiteRunner(DiscoverRunner): """Override the default django 'test' command, exclude from testing apps which we know will fail.""" def run_tests(self, test_labels, extra_tests=None, **kwargs): if not test_labels:
and in my settings:
TEST_RUNNER = 'config.test_runner.ExcludeAppsTestSuiteRunner'
I also tried using django-nose with django-nose-exclude
I read a lot about how to speed up the test itself, but did not find any instructions on how to optimize or avoid initializing the database. I saw suggestions for trying not to test the database, but I cannot or do not know how to completely avoid this.
Please let me know if
- This is normal and expected.
- Not expected (and hopefully correct or lead to what to do)
Again, I don't need help on how to speed up the test itself, but initialize (or overhead). I want the above example to execute 10 seconds instead of 80 seconds.
Thank you very much
I run the test (for one application) with --verbose 3 and find that all this is related to migrations:
Rendering model states... DONE (40.500s) Applying authentication.0001_initial... OK (0.005s) Applying account.0001_initial... OK (0.022s) Applying account.0002_email_max_length... OK (0.016s) Applying contenttypes.0001_initial... OK (0.024s) Applying contenttypes.0002_remove_content_type_name... OK (0.048s) Applying s3video.0001_initial... OK (0.021s) Applying s3picture.0001_initial... OK (0.052s) ... Many more like this
I crushed all my migrations, but still slowly.