How can I call a custom Django manage.py command directly from a test driver?

Viewed 83139

I want to write a unit test for a Django manage.py command that does a backend operation on a database table. How would I invoke the management command directly from code?

I don't want to execute the command on the Operating System's shell from tests.py because I can't use the test environment set up using manage.py test (test database, test dummy email outbox, etc...)

6 Answers

The best way to test such things - extract needed functionality from command itself to standalone function or class. It helps to abstract from "command execution stuff" and write test without additional requirements.

But if you by some reason cannot decouple logic form command you can call it from any code using call_command method like this:

from django.core.management import call_command

call_command('my_command', 'foo', bar='baz')

The Django documentation on the call_command fails to mention that out must be redirected to sys.stdout. The example code should read:

from django.core.management import call_command
from django.test import TestCase
from django.utils.six import StringIO
import sys

class ClosepollTest(TestCase):
    def test_command_output(self):
        out = StringIO()
        sys.stdout = out
        call_command('closepoll', stdout=out)
        self.assertIn('Expected output', out.getvalue())

The advanced way to run manage command with a flexible arguments and captured output

argv = self.build_argv(short_dict=kwargs)
cmd = self.run_manage_command_raw(YourManageCommandClass, argv=argv)
# Output is saved cmd.stdout.getvalue() / cmd.stderr.getvalue()

Add code to your base Test class

    @classmethod
    def build_argv(cls, *positional, short_names=None, long_names=None, short_dict=None, **long_dict):
        """
        Build argv list which can be provided for manage command "run_from_argv"
        1) positional will be passed first as is
        2) short_names with be passed after with one dash (-) prefix
        3) long_names with be passed after with one tow dashes (--) prefix
        4) short_dict with be passed after with one dash (-) prefix key and next item as value
        5) long_dict with be passed after with two dashes (--) prefix key and next item as value
        """
        argv = [__file__, None] + list(positional)[:]

        for name in short_names or []:
            argv.append(f'-{name}')

        for name in long_names or []:
            argv.append(f'--{name}')

        for name, value in (short_dict or {}).items():
            argv.append(f'-{name}')
            argv.append(str(value))

        for name, value in long_dict.items():
            argv.append(f'--{name}')
            argv.append(str(value))

        return argv

    @classmethod
    def run_manage_command_raw(cls, cmd_class, argv):
        """run any manage.py command as python object"""
        command = cmd_class(stdout=io.StringIO(), stderr=io.StringIO())
        
        with mock.patch('django.core.management.base.connections.close_all'):  
            # patch to prevent closing db connecction
            command.run_from_argv(argv)
        return command
Related