From 3e82b1093386dc0abe2b708b66f3404b1f8e74b2 Mon Sep 17 00:00:00 2001 From: "d.kovalenko" Date: Wed, 14 Oct 2026 12:41:12 +0300 Subject: [PATCH] fix: (TestServices) usage of traceback.format_exception is corrected We have a problem on py3.9-. --- tests/conftest_helpers.py | 22 +++++++--- tests/own/TestServices/__init__.py | 0 ...1__Helper__ExceptionToHumanText__Single.py | 22 ++++++++++ ...02__Helper__ExceptionToHumanText__Chain.py | 43 +++++++++++++++++++ tests/own/__init__.py | 0 5 files changed, 81 insertions(+), 6 deletions(-) create mode 100644 tests/own/TestServices/__init__.py create mode 100644 tests/own/TestServices/test_set001__Helper__ExceptionToHumanText__Single.py create mode 100644 tests/own/TestServices/test_set002__Helper__ExceptionToHumanText__Chain.py create mode 100644 tests/own/__init__.py diff --git a/tests/conftest_helpers.py b/tests/conftest_helpers.py index 099823a..ec6ef68 100644 --- a/tests/conftest_helpers.py +++ b/tests/conftest_helpers.py @@ -709,9 +709,8 @@ def Helper__ExceptionToHumanText__Single(exc: BaseException) -> str: err_msg = "Exception ({}).".format(type(exc).__name__) assert type(err_msg) is str - exc_info_lines = traceback.format_exception(exc) - assert type(exc_info_lines) is list - err_msg2 = "".join(exc_info_lines).strip() + err_msg2 = __class__.Helper__GetTraceBack(exc) + assert type(err_msg2) is str if err_msg2 != "": err_msg += " " + err_msg2 @@ -793,10 +792,21 @@ def Helper__GetExcMsg(exc: BaseException) -> str: assert isinstance(exc, BaseException) if isinstance(exc, AssertionError): - exc_info_lines = traceback.format_exception(exc) - assert type(exc_info_lines) is list - return "".join(exc_info_lines).strip() + return __class__.Helper__GetTraceBack(exc) return str(exc) + # -------------------------------------------------------------------- + @staticmethod + def Helper__GetTraceBack(exc: BaseException) -> str: + assert isinstance(exc, BaseException) + + exc_info_lines = traceback.format_exception( + type(exc), + exc, + exc.__traceback__, + ) + assert type(exc_info_lines) is list + return "".join(exc_info_lines).strip() + # ///////////////////////////////////////////////////////////////////////////// diff --git a/tests/own/TestServices/__init__.py b/tests/own/TestServices/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/own/TestServices/test_set001__Helper__ExceptionToHumanText__Single.py b/tests/own/TestServices/test_set001__Helper__ExceptionToHumanText__Single.py new file mode 100644 index 0000000..323f570 --- /dev/null +++ b/tests/own/TestServices/test_set001__Helper__ExceptionToHumanText__Single.py @@ -0,0 +1,22 @@ +from ...conftest_helpers import TestServices + +import pytest +import logging + + +class TestSet001__Helper__ExceptionToHumanText__Single: + def test_001(self): + with pytest.raises(expected_exception=AssertionError) as x: + v = 1 + assert v == 2 + + assert isinstance(x.value, AssertionError) + + msg = TestServices.Helper__ExceptionToHumanText__Single(x.value) + + logging.info("msg: {}".format(msg)) + + assert "Exception (AssertionError)." in msg + assert "v == 2" in msg + assert "AssertionError: assert 1 == 2" in msg + return diff --git a/tests/own/TestServices/test_set002__Helper__ExceptionToHumanText__Chain.py b/tests/own/TestServices/test_set002__Helper__ExceptionToHumanText__Chain.py new file mode 100644 index 0000000..704ecf2 --- /dev/null +++ b/tests/own/TestServices/test_set002__Helper__ExceptionToHumanText__Chain.py @@ -0,0 +1,43 @@ +from ...conftest_helpers import TestServices + +import pytest +import logging + + +class TestSet002__Helper__ExceptionToHumanText__Chain: + def test_001(self): + with pytest.raises(expected_exception=AssertionError) as x: + v = 1 + assert v == 2 + + assert isinstance(x.value, AssertionError) + + msg = TestServices.Helper__ExceptionToHumanText__Chain(x.value) + + logging.info("msg: {}".format(msg)) + + assert "---- 1. Exception (AssertionError):" in msg + assert "v == 2" in msg + assert "AssertionError: assert 1 == 2" in msg + return + + def test_002(self): + with pytest.raises(expected_exception=AssertionError) as x: + v = 1 + try: + raise RuntimeError("aaa") + except Exception: + assert v == 2 + + assert isinstance(x.value, AssertionError) + + msg = TestServices.Helper__ExceptionToHumanText__Chain(x.value) + + logging.info("msg: {}".format(msg)) + + assert "---- 1. Exception (RuntimeError):" in msg + assert "aaa" in msg + assert "---- 2. Exception (AssertionError):" in msg + assert "v == 2" in msg + assert "AssertionError: assert 1 == 2" in msg + return diff --git a/tests/own/__init__.py b/tests/own/__init__.py new file mode 100644 index 0000000..e69de29