3
from django.core import management
4
from django.core.management.base import CommandError
5
from django.test import TestCase
6
from django.utils import translation
7
from django.utils.six import StringIO
10
class CommandTests(TestCase):
11
def test_command(self):
13
management.call_command('dance', stdout=out)
14
self.assertEqual(out.getvalue(),
15
"I don't feel like dancing Rock'n'Roll.\n")
17
def test_command_style(self):
19
management.call_command('dance', style='Jive', stdout=out)
20
self.assertEqual(out.getvalue(),
21
"I don't feel like dancing Jive.\n")
23
def test_language_preserved(self):
25
with translation.override('fr'):
26
management.call_command('dance', stdout=out)
27
self.assertEqual(translation.get_language(), 'fr')
29
def test_explode(self):
30
""" Test that an unknown command raises CommandError """
31
self.assertRaises(CommandError, management.call_command, ('explode',))
33
def test_system_exit(self):
34
""" Exception raised in a command should raise CommandError with
35
call_command, but SystemExit when run from command line
37
with self.assertRaises(CommandError):
38
management.call_command('dance', example="raise")
39
old_stderr = sys.stderr
40
sys.stderr = err = StringIO()
42
with self.assertRaises(SystemExit):
43
management.ManagementUtility(['manage.py', 'dance', '--example=raise']).execute()
45
sys.stderr = old_stderr
46
self.assertIn("CommandError", err.getvalue())