tests.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import sys
  2. from StringIO import StringIO
  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. class CommandTests(TestCase):
  8. def test_command(self):
  9. out = StringIO()
  10. management.call_command('dance', stdout=out)
  11. self.assertEqual(out.getvalue(),
  12. "I don't feel like dancing Rock'n'Roll.\n")
  13. def test_command_style(self):
  14. out = StringIO()
  15. management.call_command('dance', style='Jive', stdout=out)
  16. self.assertEqual(out.getvalue(),
  17. "I don't feel like dancing Jive.\n")
  18. def test_language_preserved(self):
  19. out = StringIO()
  20. with translation.override('fr'):
  21. management.call_command('dance', stdout=out)
  22. self.assertEqual(translation.get_language(), 'fr')
  23. def test_explode(self):
  24. """ Test that an unknown command raises CommandError """
  25. self.assertRaises(CommandError, management.call_command, ('explode',))
  26. def test_system_exit(self):
  27. """ Exception raised in a command should raise CommandError with
  28. call_command, but SystemExit when run from command line
  29. """
  30. with self.assertRaises(CommandError):
  31. management.call_command('dance', example="raise")
  32. old_stderr = sys.stderr
  33. sys.stderr = err = StringIO()
  34. try:
  35. with self.assertRaises(SystemExit):
  36. management.ManagementUtility(['manage.py', 'dance', '--example=raise']).execute()
  37. finally:
  38. sys.stderr = old_stderr
  39. self.assertIn("CommandError", err.getvalue())