How to test custom django-admin commands

I created custom django-admin commands

But, I don't know how to test it in standard django tests


Solution 1:

If you're using some coverage tool it would be good to call it from the code with:

from django.core.management import call_command
from django.test import TestCase

class CommandsTestCase(TestCase):
    def test_mycommand(self):
        " Test my custom command."

        args = []
        opts = {}
        call_command('mycommand', *args, **opts)

        # Some Asserts.

From the official documentation

Management commands can be tested with the call_command() function. The output can be redirected into a StringIO instance

Solution 2:

You should make your actual command script the minimum possible, so that it just calls a function elsewhere. The function can then be tested via unit tests or doctests as normal.

Solution 3:

you can see in github.com example see here

def test_command_style(self):
    out = StringIO()
    management.call_command('dance', style='Jive', stdout=out)
    self.assertEquals(out.getvalue(),
        "I don't feel like dancing Jive.")

Solution 4:

To add to what has already been posted here. If your django-admin command passes a file as parameter, you could do something like this:

from django.test import TestCase
from django.core.management import call_command
from io import StringIO
import os


class CommandTestCase(TestCase):
    def test_command_import(self):
        out = StringIO()
        call_command(
            'my_command', os.path.join('path/to/file', 'my_file.txt'),
            stdout=out
        )
        self.assertIn(
        'Expected Value',
            out.getvalue()
        )

This works when your django-command is used in a manner like this:

$ python manage.py my_command my_file.txt