diff options
-rw-r--r-- | cmd2/cmd2.py | 15 | ||||
-rw-r--r-- | tests/test_transcript.py | 4 |
2 files changed, 15 insertions, 4 deletions
diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py index f54a0652..9d36e1b4 100644 --- a/cmd2/cmd2.py +++ b/cmd2/cmd2.py @@ -3683,12 +3683,23 @@ class Cmd(cmd.Cmd): self.__class__.testfiles = callargs sys.argv = [sys.argv[0]] # the --test argument upsets unittest.main() testcase = TestMyAppCase() - runner = unittest.TextTestRunner() + stream = utils.StdSim(sys.stderr) + runner = unittest.TextTestRunner(stream=stream) test_results = runner.run(testcase) if test_results.wasSuccessful(): + self.decolorized_write(sys.stderr, stream.read()) self.poutput('Tests passed', color=Fore.LIGHTGREEN_EX) else: - self.perror('Tests failed', traceback_war=False) + # Strip off the initial trackeback which isn't particularly useful for end users + error_str = stream.read() + end_of_trace = error_str.find('AssertionError:') + file_offset = error_str[end_of_trace:].find('File ') + start = end_of_trace + file_offset + + # But print the transcript file name and line number followed by what was expected and what was observed + self.perror(error_str[start:], traceback_war=False) + + # Return a failure error code to support automated transcript-based testing self.exit_code = -1 def async_alert(self, alert_msg: str, new_prompt: Optional[str] = None) -> None: # pragma: no cover diff --git a/tests/test_transcript.py b/tests/test_transcript.py index 7a2bc38a..70c9119c 100644 --- a/tests/test_transcript.py +++ b/tests/test_transcript.py @@ -269,8 +269,8 @@ def test_transcript_failure(request, capsys): assert sys_exit_code != 0 # Check for the unittest "OK" condition for the 1 test which ran - expected_start = "F\n======================================================================\nFAIL: runTest" - expected_end = "s\n\nFAILED (failures=1)\nTests failed\n" + expected_start = "File " + expected_end = "s\n\nFAILED (failures=1)\n\n" _, err = capsys.readouterr() assert err.startswith(expected_start) assert err.endswith(expected_end) |