128π
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
23π
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.
- [Django]-Django contrib admin default admin and password
- [Django]-How to group by AND aggregate with Django
- [Django]-VueJS + Django Channels
20π
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.")
- [Django]-How to send a correct authorization header for basic authentication
- [Django]-Proper way to handle multiple forms on one page in Django
- [Django]-Django: TemplateDoesNotExist (rest_framework/api.html)
7π
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
- [Django]-Django proxy model and ForeignKey
- [Django]-Check if OneToOneField is None in Django
- [Django]-Django DB Settings 'Improperly Configured' Error
-1π
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')
- [Django]-Define css class in django Forms
- [Django]-Django manage.py runserver invalid syntax
- [Django]-Django auto_now and auto_now_add
-3π
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.
- [Django]-How do you filter a nested serializer in Django Rest Framework?
- [Django]-How to get the current language in Django?
- [Django]-Where should signal handlers live in a django project?