[Pytest-commit] [hpk42/pytest] Output on assertion failure of object comparisons could be better (issue #279)

Brianna Laugher issues-reply at bitbucket.org
Wed Mar 20 01:39:38 CET 2013


--- you can reply above this line ---

New issue 279: Output on assertion failure of object comparisons could be better
https://bitbucket.org/hpk42/pytest/issue/279/output-on-assertion-failure-of-object

Brianna Laugher:

I [mentioned](http://mail.python.org/pipermail/pytest-dev/2013-March/002208.html) on pytest-dev that the output when tests fail from assertions around objects, especially lists/dicts/sets of objects, could be improved. In particular, comparisons appear with diff text which has ... "Skipping x identical leading characters in diff" (ie truncated) but there is no way to get the "full diff" - the truncated diff is usually fine when comparing strings directly but you need the full string to understand it properly when comparing, say, dicts.

[testfixtures.compare](http://mail.python.org/pipermail/pytest-dev/2013-March/002208.html) is more verbose, could be a useful starting point.

Here is an example that might be useful.


```
#!python

from collections import namedtuple
import datetime
from testfixtures import compare

TR = namedtuple('TR', 'start end')


def test_Foo():
    todayStart = datetime.datetime.today()
    todayEnd = todayStart + datetime.timedelta(hours=24)
    todayEndLater = todayEnd + datetime.timedelta(seconds=5)
    a = {frozenset('abc'): TR(todayStart, todayEndLater),
         frozenset('efg'): TR(todayStart, todayEnd)}
    b = {frozenset('abc'): TR(todayStart, todayEnd),
         frozenset('efg'): TR(todayStart, todayEnd)}
    compare(a, b)  
#    assert a == b
```

Native pytest output:


```
#!

>       assert a == b
E       assert {frozenset(['... 19, 456379))} == {frozenset(['a... 19, 456379))}
E         Skipping 121 identical leading characters in diff
E         Skipping 147 identical trailing characters in diff
E         - 1, 8, 35, 24, 456379)
E         ?           ^^
E         + 1, 8, 35, 19, 456379)
E         ?           ^^
```


testfixtures.compare output:


```
#!

>       compare(a, b)
../GFESuite-Builds/Release/release/lib/python2.7/site-packages/testfixtures/comparison.py:268: in compare
>       raise AssertionError(message)
E       AssertionError: dict not as expected:
E       
E       same:
E       [frozenset(['e', 'g', 'f'])]
E       
E       values differ:
E       frozenset(['a', 'c', 'b']): TR(start=datetime.datetime(2013, 3, 20, 8, 35, 36, 430903), end=datetime.datetime(2013, 3, 21, 8, 35, 41, 430903)) != TR(start=datetime.datetime(2013, 3, 20, 8, 35, 36, 430903), end=datetime.datetime(2013, 3, 21, 8, 35, 36, 430903))
```

At least with the latter I have a clue about which key/value is causing the problem.

This is with pytest-2.2.3 but I don't think it's improved in 2.3.


--

This is an issue notification from bitbucket.org. You are receiving
this either because you are the owner of the issue, or you are
following the issue.


More information about the pytest-commit mailing list