[IPython-user] magic function for debugging unittests

Fernando Perez Fernando.Perez at colorado.edu
Thu May 19 11:12:11 CDT 2005

danny shevitz wrote:
> 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.

Great, many thanks.

> 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. 

Yup, that's how it should be done.  In this context, the overhead of the extra 
  import is negligible (all imports after the first are very fast, nearly 
no-ops), but pulling all that upon startup would only unnecessarily slow 
things down.

Good job, and thanks again


More information about the IPython-user mailing list