15

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
  • FYI: pytest is doing that out of the box (with small comparisons of strings, lists, and dictionaries). Commented Mar 2, 2022 at 3:00

3 Answers 3

24

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

Sign up to request clarification or add additional context in comments.

1 Comment

Nope, it is still "<bla.bla.bla object at 0x1068ad5a0>.uidNumber == 1001" instead of something like "expected 1001, got 1002"
6

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.

3 Comments

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.
Just noticed why you said they were ugly. Parentheses and no ==...yuck.
@Pickels Nowadays you can use pytest. It encourages bare asserts but performs some code analysis to prettify the failure output, so you get the best of both worlds.
1

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

4 Comments

Super! I wonder, if you add a third parameter - test description, that is - you don't even need to define a separate method for each simple test, do you? Basically, it'll look like equivalent of Perl's ok() method from Test::More.
@badbishop The method I described could work for all the tests. But yes, it could be customized by adding a third parameter with the message (could be optional with the default message being the one I described).
Always shocked by the amount of stuff that is left undone in python
@RobertMoskal Python is not assuming any behavior for you (like Perl would do like for example when doing "123" + 4 and get 127 as a result :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.