A better unittest

Duncan Booth duncan at NOSPAMrcp.co.uk
Thu Apr 17 11:24:00 EDT 2003


Duncan Booth <duncan at NOSPAMrcp.co.uk> wrote in
news:Xns9360A249561CBduncanrcpcouk at 127.0.0.1: 

> The difference code could actually be made much faster quite easily.
> If I added a search for the first difference, then I could do the
> ellipsis and truncating first and only pass what is left to difflib.

Old version took 0.18 seconds on Python 2.3, gave up waiting on Python 2.2. 
This version takes 0.18 on Python 2.2, 0.08 on Python 2.3.

---- unittest.diff ---
*** unittest.py.orig	Thu Apr 17 10:08:03 2003
--- unittest.py	Thu Apr 17 16:18:54 2003
***************
*** 146,152 ****
  
  class TestFailed(Exception):
      pass
!   
  class TestCase:
      """A class whose instances are single test cases.
  
--- 146,202 ----
  
  class TestFailed(Exception):
      pass
! 
! def shortdiff(x,y):
!     '''shortdiff(x,y)
! 
!     Compare strings x and y and display differences.
!     If the strings are too long, shorten them to fit
!     in one line, while still keeping at least some difference.
!     '''
!     import difflib
!     LINELEN = 79
!     def limit(s):
!         if len(s) > LINELEN:
!             return s[:LINELEN-3] + '...'
!         return s
! 
!     def firstdiff(s, t):
!         for pos in range(0, max(len(s), len(t)), 100):
!             if s[pos:pos+100] != t[pos:pos+100]:
!                 for index in range(pos, pos+100):
!                     if s[index:index+1] != t[index:index+1]:
!                         return index
! 
!     left = LINELEN/4
!     index = firstdiff(x, y)
!     if index > left + 7:
!         x = x[:left] + '...' + x[index-4:index+LINELEN]
!         y = y[:left] + '...' + y[index-4:index+LINELEN]
!     else:
!         x, y = s[:LINELEN+1], t[:LINELEN+1]
! 
!     d = difflib.Differ()
!     diffs = list(d._fancy_replace([x], 0, 1, [y], 0, 1))
!     if len(diffs) == 3:
!         if diffs[1].startswith('+ '):
!             diffs.insert(1, '')
!         else:
!             diffs.append('')
!     if len(diffs) != 4:
!         # String were the same!
!         #return '\n'.join(diffs)
!         return limit(diffs[0][2:])
! 
!     # Remove '- ', '+ ', '? ' markers from start of lines,
!     # and newlines from end.
!     diffs = [ s[2:].rstrip() for s in diffs]
!     if max([len(s) for s in diffs]) < LINELEN:
!         return '\n'.join(diffs)
! 
!     diffs = [ limit(s) for s in diffs ]
!     return '\n'.join(diffs)
! 
  class TestCase:
      """A class whose instances are single test cases.
  
***************
*** 317,324 ****
             operator.
          """
          if first != second:
              raise self.failureException, \
!                   (msg or '%s != %s' % (`first`, `second`))
  
      def failIfEqual(self, first, second, msg=None):
          """Fail if the two objects are equal as determined by the '=='
--- 367,377 ----
             operator.
          """
          if first != second:
+             reprfirst, reprsecond = repr(first), repr(second)
+             if not msg and len(reprfirst) + len(reprsecond) > 60:
+                 msg = "failUnlessEqual\n" + shortdiff(reprfirst, 
reprsecond)
              raise self.failureException, \
!                   (msg or '%s != %s' % (reprfirst, reprsecond))
  
      def failIfEqual(self, first, second, msg=None):
          """Fail if the two objects are equal as determined by the '=='
----------------------

-- 
Duncan Booth                                             duncan at rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?




More information about the Python-list mailing list