简体   繁体   中英

Why exception isn't printed in except block

I'm trying to assert a message from an except block, something like this:

assert exceptions.main() == f"Join into Exception: {exceptions.CustomException}"

I'm using these files:

# main file
class CustomException(Exception):
    """
    This is a custom exception
    """

def return_an_exception():
    pass

def another_func():
    pass

def main():
    try:
        return_an_exception()
        another_func()
    except CustomException as e:
        return f"Join into Exception: {str(e)}"
    return "Not join into Exception"
# test file
import exceptions


def test_exceptions(
    mocker,
):
    mocker.patch("exceptions.return_an_exception").side_effect = exceptions.CustomException 
    mocker.patch("exceptions.another_func")
    assert exceptions.main() == f"Join into Exception: {exceptions.CustomException}"

I'm getting this error:

========================================================================================== FAILURES ==========================================================================================
______________________________________________________________________________________ test_exceptions _______________________________________________________________________________________

mocker = <pytest_mock.plugin.MockerFixture object at 0x102413f40>

    def test_exceptions(
        mocker,
    ):
        mocker.patch("exceptions.return_an_exception").side_effect = exceptions.CustomException
        mocker.patch("exceptions.another_func")
>       assert exceptions.main() == f"Join into Exception: {exceptions.CustomException}"
E       assert 'Join into Exception: ' == "Join into Ex...omException'>"
E         - Join into Exception: <class 'exceptions.CustomException'>
E         + Join into Exception:

test_exceptions.py:9: AssertionError
================================================================================== short test summary info ===================================================================================
FAILED test_exceptions.py::test_exceptions - assert 'Join on Exception: ' == "Join on Exce...omException'>"
===================================================================================== 1 failed in 0.06s ======================================================================================

Could you help me understand why e isn't printed

That's because the default implementation of the __str__ method of an Exception is to return its message:

print(Exception())                    # Prints nothing
print(Exception("An error occured"))  # Prints "An error occured"

You can fix this by printing e.__class__.__name__ instead:

class CustomException(Exception):
    pass

def return_an_exception():
    raise CustomException

def another_func():
    pass

def main():
    try:
        return_an_exception()
        another_func()
    except CustomException as e:
        return f"Join into Exception: {e.__class__.__name__}"
    return "Not join into Exception"
    
print(main())  # Now prints "Join into Exception: CustomException"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM