[IPython-user] magic function for debugging unittests

danny shevitz danny_shevitz at yahoo.com
Thu May 19 10:51:32 CDT 2005


howdy,

I thought I would contribute to the greater good by listing a magic
function that allows you to enter the debugger if you encounter an
exception in a unittest. The code is super simple and just explicitly
constructs a unittest test suite, then uses the debug method defined in
unittest to run the suite without catching the exceptions. In ipython
make sure you have pdb on. Despite it's simplicity, I have found this
magic very useful.

The following syntaxes all work:
%debug testModule
%debug testModule.TestCase
%debug testModule.TestCase.testMethod

I hope someone benefits from this. Note, I imported unittest from
within the function to prevent it being exposed in the global
namespace. 

Danny

########################################################################

def magic_debug(self,parameter_s=''):
    """
    run a unittest without trapping exceptions, Note: turn pdb on to
    enter the debugger
    """
    import unittest
    unittest.TestLoader().loadTestsFromName(parameter_s).debug()

# Add the new magic function to the class dict:
from IPython.iplib import InteractiveShell
InteractiveShell.magic_debug = magic_debug
del magic_debug




		
Yahoo! Mail
Stay connected, organized, and protected. Take the tour:
http://tour.mail.yahoo.com/mailtour.html




More information about the IPython-user mailing list