Skip to content

Instantly share code, notes, and snippets.

@djromero
Created February 6, 2017 20:17
Show Gist options
  • Save djromero/0c3c1c40e247dd14b43e03bcb5aca600 to your computer and use it in GitHub Desktop.
Save djromero/0c3c1c40e247dd14b43e03bcb5aca600 to your computer and use it in GitHub Desktop.
Debug on exceptions decorator.
# http://stackoverflow.com/a/4399246/143097
import unittest
import sys
import pdb
import functools
import traceback
def debug_on(*exceptions):
if not exceptions:
exceptions = (AssertionError, )
def decorator(f):
@functools.wraps(f)
def wrapper(*args, **kwargs):
try:
return f(*args, **kwargs)
except exceptions:
info = sys.exc_info()
traceback.print_exception(*info)
pdb.post_mortem(info[2])
return wrapper
return decorator
class tests(unittest.TestCase):
@debug_on()
def test_trigger_pdb(self):
assert False
@djromero
Copy link
Author

djromero commented Feb 6, 2017

For nosetests just:

nosetests --pdb

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment