How to change the message in a Python AssertionError?

前端 未结 5 431
暖寄归人
暖寄归人 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:42

    You can pass the desired message when creating the exception.

    raise AssertionError(line1 + ' != ' + line2)
    

    Hope this helps.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-08 00:54

    You want to take the caught exception, convert it to a string, combine it with some additional string info, and raise a new exception.

    x = 3
    y = 5
    try:
        assert( x == y )
    except AssertionError, e:
        raise( AssertionError( "Additional info. %s"%e ) )
    
    0 讨论(0)
  • 2020-12-08 01:00

    With this method I was able to edit the message and still have the stack trace (+any other information) visible. Also newlines are displayed right.

    try:
       my_assert_equal(line1, line2)
    except AssertionError as e:
       message = e.args[0]
       message += "\nThis appends the default message and can have newlines"
       e.args = (message,) #wrap it up in new tuple
       raise
    
    0 讨论(0)
  • 2020-12-08 01:01
    assert expression, info
    

    For instance,

    >>> assert False, "Oopsie"
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    AssertionError: Oopsie
    

    From the docs:

    Assert statements are a convenient way to insert debugging assertions into a program:

    assert_stmt ::=  "assert" expression
    ["," expression] 
    

    The simple form, assert expression, is equivalent to

    if __debug__:
        if not expression:
            raise AssertionError 
    

    The extended form

    assert expression1, expression2
    

    is equivalent to

    if __debug__:
        if not expression1:
            raise AssertionError(expression2)
    

    These equivalences assume that __debug__ and AssertionError refer to the built-in variables with those names. In the current implementation, the built-in variable __debug__ is True under normal circumstances, False when optimization is requested (command line option -O). The current code generator emits no code for an assert statement when optimization is requested at compile time. Note that it is unnecessary to include the source code for the expression that failed in the error message; it will be displayed as part of the stack trace.

    0 讨论(0)
提交回复
热议问题