How do you debug when a unittest.TestCase fails?

Emin.shopper Martinian.shopper emin.shopper at gmail.com
Thu Jul 19 15:25:50 CEST 2007


After poking around the unittest source code, the best solution I could come
up with was to do

>>> import unittest; unittest.TestCase.run = lambda self,*args,**kw:
unittest.TestCase.debug(self)

before running my tests. That patches things so that I can use pdb.pm() when
a test fails. Still, that seems like an ugly hack and I would think there is
a better solution...

Thanks,
-Emin


On 7/18/07, Emin.shopper Martinian.shopper <emin.shopper at gmail.com> wrote:
>
> Thanks for the reply, but neither of those work for me. I don't seem to
> have the "trial" program installed. Where do you get it?
>
> Also, when I use the try/catch block, I get the following error:
>
> Traceback (most recent call last):
>   File "_test.py", line 10, in <module>
>     pdb.pm()
>   File "c:\python25\lib\pdb.py", line 1148, in pm
>     post_mortem(sys.last_traceback)
> AttributeError: 'module' object has no attribute 'last_traceback'
>
>
>  On 7/18/07, Jean-Paul Calderone <exarkun at divmod.com> wrote:
> >
> > 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
> > --
> > http://mail.python.org/mailman/listinfo/python-list
> >
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20070719/a85de8e3/attachment.html>


More information about the Python-list mailing list