How to change the message in a Python AssertionError?

前端 未结 5 457
暖寄归人
暖寄归人 2020-12-07 23:57

I\'m writing per the following, in which I try to produce a decent error message when comparing two multiline blocks of Unicode text. The interior method that does the compa

5条回答
  •  再見小時候
    2020-12-08 00:46

    Use e.args, e.message is deprecated.

    try:
        assert False, "Hello!"
    except AssertionError as e:
        e.args += ('some other', 'important', 'information', 42)
        raise
    

    This preserves the original traceback. Its last part then looks like this:

    AssertionError: ('Hello!', 'some other', 'important', 'information', 42)
    

    Works in both Python 2.7 and Python 3.

提交回复
热议问题