diff options
| author | Loic Bistuer <loic.bistuer@gmail.com> | 2014-10-22 00:15:10 +0700 |
|---|---|---|
| committer | Loic Bistuer <loic.bistuer@gmail.com> | 2014-10-22 09:25:50 +0700 |
| commit | 494ba051bb0d3ebbdbea7598251b1ee6fe9b69b4 (patch) | |
| tree | 47c302bf0d5b9429efea854add58707ff0e54f25 /tests/admin_scripts/tests.py | |
| parent | 533532302ae842c95cf7294ef6cd7f3e2bfaca65 (diff) | |
Made testing of stdout and stderr more consistent.
Refs #23663.
Diffstat (limited to 'tests/admin_scripts/tests.py')
| -rw-r--r-- | tests/admin_scripts/tests.py | 53 |
1 files changed, 24 insertions, 29 deletions
diff --git a/tests/admin_scripts/tests.py b/tests/admin_scripts/tests.py index f2c0d598cf..eabaf5fc15 100644 --- a/tests/admin_scripts/tests.py +++ b/tests/admin_scripts/tests.py @@ -1524,40 +1524,36 @@ class CommandTypes(AdminScriptTestCase): Test run_from_argv properly terminates even with custom execute() (#19665) Also test proper traceback display. """ - command = BaseCommand() + err = StringIO() + command = BaseCommand(stderr=err) def raise_command_error(*args, **kwargs): raise CommandError("Custom error") - old_stderr = sys.stderr - sys.stderr = err = StringIO() - try: - command.execute = lambda args: args # This will trigger TypeError + command.execute = lambda args: args # This will trigger TypeError - # If the Exception is not CommandError it should always - # raise the original exception. - with self.assertRaises(TypeError): - command.run_from_argv(['', '']) + # If the Exception is not CommandError it should always + # raise the original exception. + with self.assertRaises(TypeError): + command.run_from_argv(['', '']) - # If the Exception is CommandError and --traceback is not present - # this command should raise a SystemExit and don't print any - # traceback to the stderr. - command.execute = raise_command_error - err.truncate(0) - with self.assertRaises(SystemExit): - command.run_from_argv(['', '']) - err_message = err.getvalue() - self.assertNotIn("Traceback", err_message) - self.assertIn("CommandError", err_message) + # If the Exception is CommandError and --traceback is not present + # this command should raise a SystemExit and don't print any + # traceback to the stderr. + command.execute = raise_command_error + err.truncate(0) + with self.assertRaises(SystemExit): + command.run_from_argv(['', '']) + err_message = err.getvalue() + self.assertNotIn("Traceback", err_message) + self.assertIn("CommandError", err_message) - # If the Exception is CommandError and --traceback is present - # this command should raise the original CommandError as if it - # were not a CommandError. - err.truncate(0) - with self.assertRaises(CommandError): - command.run_from_argv(['', '', '--traceback']) - finally: - sys.stderr = old_stderr + # If the Exception is CommandError and --traceback is present + # this command should raise the original CommandError as if it + # were not a CommandError. + err.truncate(0) + with self.assertRaises(CommandError): + command.run_from_argv(['', '', '--traceback']) def test_run_from_argv_non_ascii_error(self): """ @@ -1567,9 +1563,8 @@ class CommandTypes(AdminScriptTestCase): def raise_command_error(*args, **kwargs): raise CommandError("Erreur personnalisée") - command = BaseCommand() + command = BaseCommand(stderr=StringIO()) command.execute = raise_command_error - command.stderr = StringIO() with self.assertRaises(SystemExit): command.run_from_argv(['', '']) |
