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

SourceForge.net noreply at sourceforge.net
Sun Apr 10 20:53:41 CEST 2005


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

Category: None
Group: None
>Status: Closed
>Resolution: Rejected
Priority: 5
Submitted By: Duncan Booth (duncanb)
Assigned to: Steve Purcell (purcell)
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: Steve Purcell (purcell)
Date: 2005-04-10 19:53

Message:
Logged In: YES 
user_id=21477

The idea of using difflib to contract the failure   
messages is a great one.  Having had some   
exposure to GUI front-ends for JUnit, such as those   
available in IDEA and Eclipse, it seems to me that all   
the information available at the point of failure should   
be stored, and only formatted for display by the   
TestRunner at a later point. Eclipse, for example, can  
show long expected and actual value panes in a  
colour-code side-by-side diff viewer, which is even  
better than the difflib output.  
  
Neither the current nor the patched version of unittest  
take the approach of delegating failure message  
formatting to the Runner, but I'd prefer to see things  
move in that direction that to merge this patch in its  
current form.  
  
I guess that a richer hierarchy of failure exceptions  
would be required, so that TestRunners could elect  
to treat specific failures in their own preferred way. 

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

Comment By: Raymond Hettinger (rhettinger)
Date: 2004-12-20 03:25

Message:
Logged In: YES 
user_id=80475

Steve, do you care to pronounce on this?

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

Comment By: Raymond Hettinger (rhettinger)
Date: 2003-05-06 08: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 11: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 11: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


More information about the Patches mailing list