Skip to content

Instantly share code, notes, and snippets.

@bennett39
Created September 27, 2021 10:54
Show Gist options
  • Save bennett39/5123d3c619169cb02af42d330ff9c599 to your computer and use it in GitHub Desktop.
Save bennett39/5123d3c619169cb02af42d330ff9c599 to your computer and use it in GitHub Desktop.
Testing Django Admin Commands
from io import StringIO
from django.core.management import call_command
from django.test import TestCase
class MyCommandTest(TestCase):
def test_my_command(self):
out = StringIO() # <-- Use StringIO to capture the output of the command from stdout
args = [] # <-- Any command line arguments go here
kwargs = {'stdout': out} # <-- Any named command line options (e.g. --my-option) go here
call_command('my_command', *args, **kwargs)
# Add assertions here...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment