[Patches] [ python-Patches-725569 ] Improved output for unittest failUnlessEqual

SourceForge.net noreply@sourceforge.net
Tue, 06 May 2003 00:27:31 -0700


Patches item #725569, was opened at 2003-04-22 05:33
Message generated for change (Comment added) made by rhettinger
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=725569&group_id=5470

Category: None
Group: None
Status: Open
Resolution: None
Priority: 5
Submitted By: Duncan Booth (duncanb)
Assigned to: Nobody/Anonymous (nobody)
Summary: Improved output for unittest failUnlessEqual

Initial Comment:
The failUnlessEqual method in unittest.TestCase 
doesn't handle well the case where the objects being 
compared have a long string representation. The 
whole repr for each object is printed no matter how 
long, and no indication is given of where any 
differences occur.

This patch uses difflib on long representations to 
provide a short output that highlights where the first 
difference actually is. It also limits the output for each 
value to fit on a single line (with the differences 
indicated on the line following).

e.g.
FAIL: test_failunlessEqual4 (__main__.Test)
----------------------------------------------------------------------
TestFailed: failUnlessEqual
{'A': 65, 'C': 67, ...0, 'P ': 0, 'R': 82, 'U': 85, 'T': 84, 'W': 
87, 'V': 8...
                   ...    ^^   ^                                            ...
{'A': 65, 'C': 67, ...0, 'S': 83, 'R': 82, 'U': 85, 'T': 84, 'W': 
87, 'V': 8...
                   ...    ^   ^^                                            ...
  File "F:\temp\test.py", line 59, in test_failunlessEqual4
    self.failUnlessEqual(d2, d1)

----------------------------------------------------------------------

The attached file contains the changes, assuming that 
patch "722638 Better output for unittest" has already 
been applied.


----------------------------------------------------------------------

>Comment By: Raymond Hettinger (rhettinger)
Date: 2003-05-06 02:27

Message:
Logged In: YES 
user_id=80475

The patch should wrap "import difflib" in a try/except and 
revert to prior behavior upon an ImportError.  This will 
make sure unittest still runs on old Pythons.

----------------------------------------------------------------------

Comment By: Duncan Booth (duncanb)
Date: 2003-04-25 05:35

Message:
Logged In: YES 
user_id=74031

Oops. I forgot to test for the case where the string didn't 
need shortening in the middle. New patch uploaded.

----------------------------------------------------------------------

Comment By: Thomas Heller (theller)
Date: 2003-04-25 05:03

Message:
Logged In: YES 
user_id=11105

There's a bug in your patch:

!     else:
!         x, y = s[:LINELEN+1], t[:LINELEN+1]
!         left = 0

s and t are not defined here (UnboundLocal exception is raised),

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=725569&group_id=5470