From 066550632f06a6a6b5d0c78ea315e131439fba46 Mon Sep 17 00:00:00 2001 From: Eleanor Boyd Date: Tue, 10 Oct 2023 09:00:05 -0700 Subject: [PATCH] fix unittest output to remove print of only object reference (#22180) the traceback object was incorrectly printed as just the reference to the object in the error message. Ended up just removing it since it is correctly printed in the traceback object which is where it should ultimately belong. closes: https://github.com/microsoft/vscode-python/issues/22181 --------- Co-authored-by: Raymond Zhao <7199958+rzhao271@users.noreply.github.com> --- .../tests/unittestadapter/test_execution.py | 3 +++ pythonFiles/unittestadapter/execution.py | 15 ++++++++++----- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/pythonFiles/tests/unittestadapter/test_execution.py b/pythonFiles/tests/unittestadapter/test_execution.py index f7306e37662e..ccf13d983c60 100644 --- a/pythonFiles/tests/unittestadapter/test_execution.py +++ b/pythonFiles/tests/unittestadapter/test_execution.py @@ -202,6 +202,9 @@ def test_failed_tests(): assert "outcome" in id_result assert id_result["outcome"] == "failure" assert "message" and "traceback" in id_result + assert "2 not greater than 3" in str(id_result["message"]) or "1 == 1" in str( + id_result["traceback"] + ) assert True diff --git a/pythonFiles/unittestadapter/execution.py b/pythonFiles/unittestadapter/execution.py index 0684ada8e44b..5f46bda95328 100644 --- a/pythonFiles/unittestadapter/execution.py +++ b/pythonFiles/unittestadapter/execution.py @@ -104,13 +104,18 @@ def formatResult( subtest: Union[unittest.TestCase, None] = None, ): tb = None - if error and error[2] is not None: - # Format traceback + + message = "" + # error is a tuple of the form returned by sys.exc_info(): (type, value, traceback). + if error is not None: + try: + message = f"{error[0]} {error[1]}" + except Exception: + message = "Error occurred, unknown type or value" formatted = traceback.format_exception(*error) + tb = "".join(formatted) # Remove the 'Traceback (most recent call last)' formatted = formatted[1:] - tb = "".join(formatted) - if subtest: test_id = subtest.id() else: @@ -119,7 +124,7 @@ def formatResult( result = { "test": test.id(), "outcome": outcome, - "message": str(error), + "message": message, "traceback": tb, "subtest": subtest.id() if subtest else None, }