How do you debug when a unittest.TestCase fails?

Jean-Paul Calderone exarkun at
Wed Jul 18 23:04:54 CEST 2007

On Wed, 18 Jul 2007 16:40:46 -0400, "Emin.shopper Martinian.shopper" <emin.shopper at> wrote:
>Dear Experts,
>How do you use pdb to debug when a TestCase object from the unittest module
>fails? Basically, I'd like to run my unit tests and invoke when
>something fails.
>I tried the following with now success:
>Imagine that I have a module that looks like the following:
>import unittest
>class MyTest(unittest.TestCase):
>    def testIt(self):
>        raise Exception('boom')
>if __name__ == '__main__':
>    unittest.main()
>If I do
>>>>import _test; _test.unittest()
>no tests get run.
>If I try
>>>>import _test; t = _test.MyTest()
>I get
>Traceback (most recent call last):
>  File "<stdin>", line 1, in <module>
>  File "c:\python25\lib\", line 209, in __init__
>    (self.__class__, methodName)
>ValueError: no such test method in <class '_test.MyTest'>: runTest
>If I try
>>>>import _test; t = _test.MyTest(methodName='testIt');
>nothing happens.

I use `trial -b <filename>', which automatically enables a bunch of nice
debugging functionality. ;)  However, you can try this, if you're not
interested in using a highly featureful test runner:

        import pdb

This will "post-mortem" the exception, a commonly useful debugging


More information about the Python-list mailing list