This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Improved output for unittest failUnlessEqual
Type: Stage:
Components: None Versions:
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: purcell Nosy List: duncanb, purcell, rhettinger, theller
Priority: normal Keywords: patch

Created on 2003-04-22 10:33 by duncanb, last changed 2022-04-10 16:08 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
unittest.diff duncanb, 2003-04-22 10:33 Patch for unittest.py
unittest.diff duncanb, 2003-04-25 10:35 Revised patch for unittest.py
Messages (6)
msg43438 - (view) Author: Duncan Booth (duncanb) Date: 2003-04-22 10:33
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.
msg43439 - (view) Author: Thomas Heller (theller) * (Python committer) Date: 2003-04-25 10:03
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),
msg43440 - (view) Author: Duncan Booth (duncanb) Date: 2003-04-25 10:35
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.
msg43441 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2003-05-06 07:27
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.
msg43442 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2004-12-20 03:25
Logged In: YES 
user_id=80475

Steve, do you care to pronounce on this?
msg43443 - (view) Author: Steve Purcell (purcell) (Python triager) Date: 2005-04-10 18:53
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. 
History
Date User Action Args
2022-04-10 16:08:16adminsetgithub: 38347
2003-04-22 10:33:41duncanbcreate