Skip to content Skip to sidebar Skip to footer

Custom Django Management Command With Appended Parameters

My team runs a custom test suite pretty regularly. When we want to run the whole thing, we do ./manage.py test --keepdb --failfast --settings=AE.test_settings When we want to run

Solution 1:

You could subclass the Django test command and override the handle method to set the options you need.

from django.core.management.commands.test import Command as BaseTestCommand


classCommand(BaseTestCommand):
    defhandle(self, *test_labels, **options):
        options['failfast'] = True
        options['keepdb'] = True
        options['settings'] = 'NJ.test_settings'super(Command, self).handle(*test_labels, **options)

As to why your code does not work, it is because your are misusing the call_command function. Every command option (that starts with --) must be passed as a keyworded argument even if it is boolean, your handle method should then be:

defhandle(self, *args, **kwargs):
    kwargs.update({
        'settings': 'NJ.test_settings',
        'keepdb': True,
        'failfast': True
    })
    management.call_command("test", *args, **kwargs)

Post a Comment for "Custom Django Management Command With Appended Parameters"