A better unittest

Thomas Heller theller at python.net
Wed Apr 16 10:02:27 EDT 2003


"Terry Reedy" <tjreedy at udel.edu> writes:

> > What do people think? Is this a useful change, and should I submit a
> > patch?
> 
> I plan to start using inittest Real Soon Now.  For normal use,  the
> irrelevant display of inittest internals constitutes noise and I
> believe I would prefer its suppression.  And yet, it is a bit
> disconcerting: the last displayed line does *not* raise the displayed
> exception.  So there is a visual 'mismatch'.  Two suggestions for
> further revision.
> 
> 1. The phrase 'most recent call last' is not longer true.  Substitute
> 'inittest internals suppressed' or somesuch.
> 
> 2. Add some visual indicator of the omission, such as an ellipsis line

> 
> This patch effectively assumes that inittest works perfectly now and
> after all future patches.  While I would make the suppression the
> default, I suspect that normal tracebacks might sometimes be a needed
> option.

These are valid points, IMO. Thanks for these suggestions.
Here is the next round. The append script prints this output,
is this better? Note that the first testcase (test_exception)
is an error, and the other three are simply failed tests.

------------------------------------------------
EFFF
======================================================================
ERROR: test_exception (__main__.FailingTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "xunit.py", line 9, in test_exception
    a = 1/0
ZeroDivisionError: integer division or modulo by zero

======================================================================
FAIL: test_failIfEqual (__main__.FailingTests)
----------------------------------------------------------------------
TestFailed: 0 == 0
  File "xunit.py", line 15, in test_failIfEqual
    self.failIfEqual(self.a, self.a)

======================================================================
FAIL: test_failUnlessEqual (__main__.FailingTests)
----------------------------------------------------------------------
TestFailed: 0 != 1
  File "xunit.py", line 12, in test_failUnlessEqual
    self.failUnlessEqual(self.a, self.b)

======================================================================
FAIL: test_failUnlessRaises (__main__.FailingTests)
----------------------------------------------------------------------
TestFailed: wrong exception: 'ValueError: 10'
  File "xunit.py", line 18, in test_failUnlessRaises
    self.failUnlessRaises(TypeError, self._raise, ValueError, 10)

----------------------------------------------------------------------
Ran 4 tests in 0.010s

FAILED (failures=3, errors=1)
------------------------------------------------

#------------------
import unittest

class FailingTests(unittest.TestCase):
    a = 0
    b = 1
    def test_exception(self):
        a = 1/0

    def test_failUnlessEqual(self):
        self.failUnlessEqual(self.a, self.b)

    def test_failIfEqual(self):
        self.failIfEqual(self.a, self.a)

    def test_failUnlessRaises(self):
        self.failUnlessRaises(TypeError, self._raise, ValueError, 10)

    # helper method
    def _raise(self, exc_class, value):
        "Helper method to raise an exception"
        raise exc_class, value

if __name__ == '__main__':
    unittest.main()
#------------------




More information about the Python-list mailing list