tags:

views:

68

answers:

2

Hello,

is it possible to show the assertion values that failed? It shows the traceback and what kind of exception was throw but it would more practical to know which values failed.

Example:

assert result.file == file
AssertionError
+1  A: 

You can catch AssertionErrors, print some debugging info, and then reraise the error with the raise command:

try:
    assert result.file == file
except AssertionError as err:
    print('result.file: {f}'.format(f=result.file))
    print(err)
    raise
unutbu
Very nice tip, thanks.
Pickels
+1  A: 

assert result.file == file, "%s != %s" % (result.file, file,)

That's why ugly self.assert<Foo> methods were introduced in unittest.TestCase instead of nice and short asserts: self.assert<Foo> methods know how to display failure messages.

By the way, I thought that nose do some black magic so in simple cases

assert a == b

should show meaningful error message.

Mike Korobov
Thanks, I should be using self.assert. You can let Nose show more info with -d. It will show the type but not the actual value.
Pickels
Just noticed why you said they were ugly. Parentheses and no ==...yuck.
Pickels