diff --git a/tests/conftest_helpers.py b/tests/conftest_helpers.py index 099823a1..ec6ef68a 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 00000000..e69de29b 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 00000000..323f5705 --- /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 00000000..704ecf28 --- /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 00000000..e69de29b