79

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.

Andres Jaan Tack
  • 22,566
  • 11
  • 59
  • 78
  • Just to clarify, I realize that the catching of an Assertion error is strange. It just so happens that `my_assert_equal` is kind of deep and I don't want to mess with it. – Andres Jaan Tack Sep 27 '10 at 21:24
  • 1
    Just to point out, you should have `except` not `catch`. Though I'm sure that's just a typo :p – Katriel Sep 27 '10 at 21:30

4 Answers4

141
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.

Katriel
  • 120,462
  • 19
  • 136
  • 170
  • 2
    Note that you can also embed newlines in the `info` string expression to make them look nice when displayed. – martineau Sep 27 '10 at 23:04
  • 1
    Also note that you can add additional information to the string expression by using Python's string interpolation and newer string formating operations. – martineau Sep 27 '10 at 23:08
  • 1
    Raising an exception in general is not a problem. It's catching, modifying, and re-raising an expression, that I asked. – Andres Jaan Tack Sep 28 '10 at 05:02
  • 1
    @Andres -- you should edit the question to say that explicitly... I don't interpret the question as saying that. – Katriel Sep 28 '10 at 13:54
  • 2
    Smacks forehead. Now it's clear why `assert(condition, message)` breaks with an always-true warning: assert is an operator not a function, and so that statement tests a tuple. Instead `assert condition message` – Bob Stein Jan 19 '15 at 16:41
76

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.

Honza Javorek
  • 8,566
  • 8
  • 47
  • 66
6

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 ) )
Russell Borogove
  • 18,516
  • 4
  • 43
  • 50
  • 4
    I didn't see any of the posted answers offer the general solution of how to add information to an exception and re-raise with both the original and additional information, which I thought was what the OP was asking. – Russell Borogove Sep 28 '10 at 01:41
  • Russel is right, this is the sort of thing I was looking for. – Andres Jaan Tack Sep 28 '10 at 05:01
4

You can pass the desired message when creating the exception.

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

Hope this helps.

zchtodd
  • 1,140
  • 10
  • 24
  • That's not what the OP means; `AssertionError` is (generally) raised by the `assert` statement. – Katriel Sep 27 '10 at 21:34
  • I'm not sure... but if wrapped with an `if` statement in place of using `assert` it might be acceptable. That way you could use an `if else` block and fallback on an AssertionError with a custom message. – Zv_oDD Nov 28 '17 at 14:31