Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the message in a Python AssertionError?

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 comparison raises an assertion, but the default explanation is useless to me

I need to add something to code such as this below:

def assert_long_strings_equal(one, other):
    lines_one = one.splitlines()
    lines_other = other.splitlines()
    for line1, line2 in zip(lines_one, lines_other):
        try:
            my_assert_equal(line1, line2)
        except AssertionError, error:
            # Add some information to the printed result of error??!
            raise

I cannot figure out how to change the printed error message in the assertionerror I catch. I always get AssertionError: u'something' != 'something else', which only shows the first line of the output.

How can I change the assertion message to print out whatever I want?

If it's relevant, I am using nose to run the test.

like image 340
Andres Jaan Tack Avatar asked Sep 27 '10 20:09

Andres Jaan Tack


People also ask

How do you change the error message in Python?

try: int("string") #the code that raises the error except ValueError: raise ValueError("Your custom message here.")

How do I resolve AssertionError in Python?

If the assertion fails, Python uses ArgumentExpression as the argument for the AssertionError. AssertionError exceptions can be caught and handled like any other exception using the try-except statement, but if not handled, they will terminate the program and produce a traceback.

What does AssertionError mean in Python?

Assertion is a programming concept used while writing a code where the user declares a condition to be true using assert statement prior to running the module. If the condition is True, the control simply moves to the next line of code.

What is assertion error?

An assertion Error is thrown when say "You have written a code that should not execute at all costs because according to you logic it should not happen. BUT if it happens then throw AssertionError. And you don't catch it." In such a case you throw an Assertion error.


4 Answers

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.

like image 178
Katriel Avatar answered Oct 17 '22 05:10

Katriel


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.

like image 35
Honza Javorek Avatar answered Oct 17 '22 05:10

Honza Javorek


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 ) )
like image 5
Russell Borogove Avatar answered Oct 17 '22 03:10

Russell Borogove


You can pass the desired message when creating the exception.

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

Hope this helps.

like image 4
zchtodd Avatar answered Oct 17 '22 05:10

zchtodd