'Exception raised in a command should raise CommandError with
call_command, but SystemExit when run from command line'

def test_system_exit(self):

    with self.assertRaises(CommandError):
        management.call_command('dance', example='raise')
    old_stderr = sys.stderr
    sys.stderr = err = StringIO()
    try:
        with self.assertRaises(SystemExit):
            management.ManagementUtility(['manage.py', 'dance', '--example=raise']).execute()
    finally:
        sys.stderr = old_stderr
    self.assertIn('CommandError', err.getvalue())
