Search code examples
pythondjangounit-testingparallel-processingparallel-testing

Setting up django parallel test in setting.py


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.


Solution

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