How to test custom django-admin commands

Viewed 23638
6 Answers

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

A simple alternative to parsing stdout is to make your management command exit with an error code if it doesn't run successfully, for example using sys.exit(1).

You can catch this in a test with:

    with self.assertRaises(SystemExit):
        call_command('mycommand')

I agree with Daniel that the actual command script should do the minimum possible but you can also test it directly in a Django unit test using os.popen4.

From within your unit test you can have a command like

fin, fout = os.popen4('python manage.py yourcommand')
result = fout.read()

You can then analyze the contents of result to test whether your Django command was successful.

Related