How to test custom django-admin commands

dixon picture dixon · Aug 17, 2009 · Viewed 18.7k times · Source

I created custom django-admin commands

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

Answer

Jorge E. Cardona picture Jorge E. Cardona · Jun 28, 2011

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