4

Hello I know that it's possible to run tests in django in parallel via --parallel flag eg. python manage.py test --parallel 10. It really speeds up testing in project I'm working for, what is really nice. But Developers in company shares different hardware setups. So ideally I would like to put parallel argument in ./app_name/settings.py so every developer would use at least 4 threads in testing or number of cores provided by multiprocessing lib.

I know that I can make another script let's say run_test.py in which I make use of --parallel, but I would love to make parallel testing 'invisible'.

To sum up - my question is: Can I put number of parallel test runs in settings of django app? And if answer is yes. There is second question - Would command line argument (X) manage.py --parallel X override settings from './app_name/settings'

Any help is much appreciated.

Alex Baranowski
  • 1,014
  • 13
  • 22

1 Answers1

4

There is no setting for this, but you can override the test command to set a different default value. In one of your installed apps, create a .management.commands submodule, and add a test.py file. In there you need to subclass the old test command:

from django.conf import settings
from django.core.management.commands.test import Command as TestCommand

class Command(TestCommand):
    def add_arguments(self, parser):
        super().add_arguments(parser)
        if hasattr(settings, 'TEST_PARALLEL_PROCESSES'):
            parser.set_defaults(parallel=settings.TEST_PARALLEL_PROCESSES)

This adds a new default to the --parallel flag. Running python manage.py test --parallel=1 will still override the default.

knbk
  • 52,111
  • 9
  • 124
  • 122
  • 1
    Works like charm! Thank you very much. Code has two little, tiny mistakes: There should be ":" in if statement and there should be 'set_defaults' instead of 'set_default'. Thank you once more, you saved hours of testing execution time :). – Alex Baranowski Jun 06 '17 at 14:33
  • 2
    @AlexBaranowski Fixed :) – knbk Jun 06 '17 at 14:37