Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python unit testing: make nose show failed assertions values

Tags:

python

nose

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
like image 785
Pickels Avatar asked Sep 01 '10 15:09

Pickels


3 Answers

You should run nosetests -d this will display the values of the objects that fail the compare in assert.

like image 132
Jorge Vargas Avatar answered Sep 20 '22 13:09

Jorge Vargas


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.

like image 29
Mikhail Korobov Avatar answered Sep 21 '22 13:09

Mikhail Korobov


Another possibility: define your own function that does the trick:

def assert_eq(obt, exp):
    assert obt==exp, "\n*Expected:\n%s\n*Obtained:\n%s" % (exp, obt)

You can call it instead of assert:

assert_eq ( self.data['SQ'].code, "SQ" )

And this returns this nice error:

AssertionError

like image 32
Jean-Francois T. Avatar answered Sep 20 '22 13:09

Jean-Francois T.