[Python-Dev] Unittest PEP do's and don'ts (BDFL pronouncement)

Robert Kern robert.kern at gmail.com
Thu Jul 17 01:45:45 CEST 2008

Guido van Rossum wrote:
> On Wed, Jul 16, 2008 at 2:03 PM, Raymond Hettinger <python at rcn.com> wrote:
>> From: "Michael Foord" <fuzzyman at voidspace.org.uk>
>>> assertIn / assertNotIn I use very regularly for collection membership
>> - self.assert_(func(x) in result_set)
>> + self.assertIn(func(x), result_set)
>> Yawn.  The gain is zero.  Actually, it's negative because the second
>> doesn't read as nicely as the pure python expression.
> I disagree. The reason why we have assertEquals(x, y) is that the
> error message can show the values of x and y, whereas assert x == y
> can't show those. Showing the values can be tremendously useful to
> debugging the failure. (Doing an intelligent comparison, e.g. a string
> or list "diff" instead of showing the two values, can be even more
> useful, and I'd be in favor of that rather than adding new methods
> like assertListsEqual.)
> (Titus asks if the assert statement could be adjusted to do better
> reporting. But that's not going to happen -- it would require a
> tremendous amount of compiler support that would have to be
> implemented in every Python implementation (last I counted there were
> at least five). In addition, python -O removes all asserts from your
> code -- that's why we use assertXxx functions in the first place.)

The assert statement itself does not have to be modified. nose and py.test are 
already capable of picking out the variables used in a failing assert expression 
and print them out. For example:

[~]$ cat test_nicefail.py
def test_nicefail():
     x = 12
     assert x == 10

[~]$ nosetests --detailed-errors test_nicefail.py
FAIL: test_nicefail.test_nicefail
Traceback (most recent call last):
line 182, in runTest
   File "/Users/rkern/test_nicefail.py", line 3, in test_nicefail
     assert x == 10
     12 = 12
 >>  assert 12 == 10

Ran 1 test in 0.044s

FAILED (failures=1)

[~]$ py.test test_nicefail.py
====================== test process starts =======================
using py lib: 
<rev unknown>

test_nicefail.py[1] F

___________________ entrypoint: test_nicefail ____________________

     def test_nicefail():
         x = 12
E       assert x == 10
 >       assert 12 == 10

============ tests finished: 1 failed in 0.09 seconds ============

Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco

More information about the Python-Dev mailing list