Thread: [Pyunit-interest] PATCH: AssertionError message
Brought to you by:
purcell
From: Alexander G. <al...@in...> - 2002-06-03 16:46:22
|
I do alot of text manipulation using python and testing with PyUnit, so many of my tests are of the sort: self.assertEqual(string1, string2), and it matters if there is a space on the end of one string and not on the other. So when PyUnit returns an error message which does not delimit the strings, I lose time trying to figure out where the difference between these seemingly similar 200+ character strings is, when the only difference is an extra space on the end of one. So I hacked unittest.py as per the following patch. Now when strings are not equal, they are displayed in quotes, so I can tell exactly where the strings begin and end. I am rather a newbie, so don't know if this will break anything. It could be made conditional to strings easily enough. Something like: if type(first) == types.StringType: first = repr(first) would work, I suppose. It seemed useful and generic enough to me to suggest it for inclusion. Feel free to beat me up for stupidity. :) Alexander --- pyunit-1.4.1/unittest.py Wed Aug 8 02:05:08 2001 +++ my_pyunit/unittest.py Mon Jun 3 11:23:03 2002 @@ -270,14 +270,14 @@ operator. """ if first != second: - raise self.failureException, (msg or '%s != %s' % (first, second)) + raise self.failureException, (msg or '%s != %s' % ( repr(first), repr(second) )) def failIfEqual(self, first, second, msg=None): """Fail if the two objects are equal as determined by the '==' operator. """ if first == second: - raise self.failureException, (msg or '%s == %s' % (first, second)) + raise self.failureException, (msg or '%s == %s' % ( repr(first), repr(second) )) assertEqual = assertEquals = failUnlessEqual |
From: Phlip <ppl...@om...> - 2002-06-03 17:39:09
|
Alexander Garden sez: > - raise self.failureException, (msg or '%s != %s' % (first, > second)) + raise self.failureException, (msg or '%s != %s' % ( > repr(first), repr(second) )) I second this patch. The repr, for strings, will reveal sneaky characters like \011 or \014 or \000. For objects, folks want __str__ to report an object's cosmetic state to end-users, but __repr__ to report its technical state. I sometimes make __repr__ return a string which, if eval'd, would construct an identical object. -- Phlip http://www.greencheese.org/HatTrick "Solutions are not the answer." -- Richard Nixon |
From: Steve P. <ste...@ya...> - 2002-06-04 06:18:08
|
A similar patch is actually in the PyUnit CVS already, and appears to be in the standard-library version of PyUnit shipped with Python 2.2, though not 2.1. The new code looks like this: def failUnlessEqual(self, first, second, msg=None): """Fail if the two objects are unequal as determined by the '!=' 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 '==' operator. """ if first == second: raise self.failureException, \ (msg or '%s == %s' % (`first`, `second`)) Is anyone interested in a fresh standalone release of PyUnit soon? I had guessed that most people have moved to Python 2.x by now, but maybe I'm mistaken? Best wishes to all, -Steve |
From: Phlip <ppl...@om...> - 2002-06-04 12:40:54
|
Steve Purcell sez: > if first != second: > raise self.failureException, \ > (msg or '%s != %s' % (`first`, `second`)) Sorry, but do back-ticks do the same as repr? I thought they did string. -- Phlip http://www.greencheese.org/EvolutionaryPsychology -- Friends don't let friends use Closed Source software -- |
From: Steve P. <ste...@ya...> - 2002-06-04 12:44:45
|
Phlip wrote: > Sorry, but do back-ticks do the same as repr? I thought they did string. Back-ticks are 'repr' in disguise, not 'str': Python 2.1.3 (#1, Apr 20 2002, 10:14:34) [GCC 2.95.4 20011002 (Debian prerelease)] on linux2 Type "copyright", "credits" or "license" for more information. >>> print `"hello"` 'hello' >>> print str("hello") hello >>> -Steve -- Steve Purcell, Pythangelist Get testing at http://pyunit.sourceforge.net/ |
From: Patrick K. O'B. <po...@or...> - 2002-06-04 12:48:40
|
[Phlip] > > if first != second: > > raise self.failureException, \ > > (msg or '%s != %s' % (`first`, `second`)) > > Sorry, but do back-ticks do the same as repr? I thought they did string. Yes, backticks (backquotes) do the same as repr. But they a bit of a wart and I wouldn't encourage their use. The code above could instead be: (msg or '%r != %r' % (first, second)) --- Patrick K. O'Brien Orbtech |
From: Steve P. <ste...@ya...> - 2002-06-04 12:57:37
|
Patrick K. O'Brien wrote: > Yes, backticks (backquotes) do the same as repr. But they a bit of a wart > and I wouldn't encourage their use. The code above could instead be: > > (msg or '%r != %r' % (first, second)) Not with Python 1.5.2. -Steve |
From: Alexander G. <al...@in...> - 2002-06-04 12:51:20
|
On Tue, Jun 04, 2002 at 05:31:30AM -0700, Phlip wrote: > Sorry, but do back-ticks do the same as repr? I thought they did string. I had the same question. Here's the relevant snippet from the docs (section 2.3): repr (object) Return a string containing a printable representation of an object. This is the same value yielded by conversions (reverse quotes). |
From: Fred L. D. Jr. <fd...@ac...> - 2002-06-04 14:53:32
|
Phlip writes: > Sorry, but do back-ticks do the same as repr? I thought they did string. They are equivalent to repr(). -Fred -- Fred L. Drake, Jr. <fdrake at acm.org> PythonLabs at Zope Corporation |