How do you debug when a unittest.TestCase fails?

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


On Wed, 18 Jul 2007 16:40:46 -0400, "Emin.shopper Martinian.shopper" <emin.shopper at gmail.com> 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 pdb.pm when
>something fails.
>
>I tried the following with now success:
>
>Imagine that I have a module _test.py 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\unittest.py", 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'); t.run()
>
>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:

    try:
        unittest.main()
    except:
        import pdb
        pdb.pm()

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

Jean-Paul



More information about the Python-list mailing list