56

Is there a way to automatically start the debugger at the point at which a unittest fails?

Right now I am just using pdb.set_trace() manually, but this is very tedious as I need to add it each time and take it out at the end.

For Example:

import unittest

class tests(unittest.TestCase):

    def setUp(self):
        pass

    def test_trigger_pdb(self):
        #this is the way I do it now
        try:
            assert 1==0
        except AssertionError:
            import pdb
            pdb.set_trace()

    def test_no_trigger(self):
        #this is the way I would like to do it:
        a=1
        b=2
        assert a==b
        #magically, pdb would start here
        #so that I could inspect the values of a and b

if __name__=='__main__':
    #In the documentation the unittest.TestCase has a debug() method
    #but I don't understand how to use it
    #A=tests()
    #A.debug(A)

    unittest.main()
0

9 Answers 9

37

I think what you are looking for is nose. It works like a test runner for unittest.

You can drop into the debugger on errors, with the following command:

nosetests --pdb
7
  • 4
    If you were to use self.assertEquals rather than plain assert, so that the test has a "failure" rather than an "error", then the command to run would be nosetest --pdb-failures.
    – jchl
    Dec 21, 2011 at 10:58
  • 1
    On win32 with python 2.7, I installed nose with easy_install nose but then found the command was nosetests not nosetest. I also had to run with --pdb-failures.
    – GrantJ
    Feb 27, 2012 at 2:58
  • On Ubuntu, I also find this wonderful command is named '''nosetests''', emphasis on the plural 's' on the end. One can install the package python-nose (sudo apt-get install python-nose) to have this handy command. To run an existing test, '''nosetests --pdb-failures ./test_set.py''' where test_set.py is your existing unit test. Aug 20, 2013 at 19:56
  • 1
    For the sucessor nose2, see answer below.
    – serv-inc
    Jan 19, 2018 at 9:07
  • 1
    Downvoted because nose is unmaintained (see nose2).
    – Nobody
    Apr 20, 2020 at 17:10
30
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 1 == 0

I corrected the code to call post_mortem on the exception instead of set_trace.

4
  • 4
    I'd also advise you to use a global flag to turn this debugging on and off. It would make running tests more complicated. I'd be pretty pissed off if I ran someone's test suite and it popped a debugger. Dec 9, 2010 at 20:10
  • Rosh, thanks. It works great. Point well taken about the global flag :)
    – tjb
    Dec 10, 2010 at 7:53
  • 1
    This is a nice solution for the moment, so +1. I would add to your code this: import pdb; import sys; and after pdb.post_mortem(...), raise. This is the rare case when the exception should be re-thrown. If the user continues a failed test case, it will otherwise be counted as passed. On another note; if only it were possible to detect pdb already running and not pop up the debugger for unittests, yet break on failures when launched under the debugger, I would be still happier. Aug 20, 2013 at 18:38
  • @HeathHunnicutt: Sounds like there may be a way to detect if the debugger is already running, see question How to detect that Python code is being executed through the debugger?.
    – martineau
    Dec 4, 2014 at 17:39
4

Third party test framework enhancements generally seem to include the feature (nose and nose2 were already mentioned in other answers). Some more:

pytest supports it.

pytest --pdb

Or if you use absl-py's absltest instead of unittest module:

name_of_test.py --pdb_post_mortem
4

A simple option is to just run the tests without result collection and letting the first exception crash down the stack (for arbitrary post mortem handling) by e.g.

try: unittest.findTestCases(__main__).debug()
except:
    pdb.post_mortem(sys.exc_info()[2])

Another option: Override unittest.TextTestResult's addError and addFailure in a debug test runner for immediate post_mortem debugging (before tearDown()) - or for collecting and handling errors & tracebacks in an advanced way.

(Doesn't require extra frameworks or an extra decorator for test methods)

Basic example:

import unittest, pdb

class TC(unittest.TestCase):
    def testZeroDiv(self):
        1 / 0

def debugTestRunner(post_mortem=None):
    """unittest runner doing post mortem debugging on failing tests"""
    if post_mortem is None:
        post_mortem = pdb.post_mortem
    class DebugTestResult(unittest.TextTestResult):
        def addError(self, test, err):
            # called before tearDown()
            traceback.print_exception(*err)
            post_mortem(err[2])
            super(DebugTestResult, self).addError(test, err)
        def addFailure(self, test, err):
            traceback.print_exception(*err)
            post_mortem(err[2])
            super(DebugTestResult, self).addFailure(test, err)
    return unittest.TextTestRunner(resultclass=DebugTestResult)

if __name__ == '__main__':
    ##unittest.main()
    unittest.main(testRunner=debugTestRunner())
    ##unittest.main(testRunner=debugTestRunner(pywin.debugger.post_mortem))
    ##unittest.findTestCases(__main__).debug()
1
  • Does that need to be unittest.findTestCases('main').debug() ? Jul 11, 2019 at 15:21
2

To apply @cmcginty's answer to the successor nose 2 (recommended by nose available on Debian-based systems via apt-get install nose2), you can drop into the debugger on failures and errors by calling

nose2

in your test directory.

For this, you need to have a suitable .unittest.cfg in your home directory or unittest.cfg in the project directory; it needs to contain the lines

[debugger]
always-on = True
errors-only = False
1
  • nose2 --debugger FTW!
    – mdaoust
    Mar 28 at 19:28
0

To address the comment in your code "In the documentation the unittest.TestCase has a debug() method but I don't understand how to use it", you can do something like this:

suite = unittest.defaultTestLoader.loadTestsFromModule(sys.modules[__name__])
suite.debug()

Individual test cases are created like: testCase = tests('test_trigger_pdb') (where tests is a sub-class of TestCase as per your example). And then you can do testCase.debug() to debug one case.

-1

Here's a built-in, no extra modules, solution:

import unittest
import sys
import pdb

####################################
def ppdb(e=None):
    """conditional debugging
       use with:  `if ppdb(): pdb.set_trace()` 
    """
    return ppdb.enabled

ppdb.enabled = False
###################################


class SomeTest(unittest.TestCase):

    def test_success(self):
        try:
            pass
        except Exception, e:
            if ppdb(): pdb.set_trace()
            raise

    def test_fail(self):
        try:
            res = 1/0
            #note:  a `nosetests --pdb` run will stop after any exception
            #even one without try/except and ppdb() does not not modify that.
        except Exception, e:
            if ppdb(): pdb.set_trace()
            raise


if __name__ == '__main__':
    #conditional debugging, but not in nosetests
    if "--pdb" in sys.argv:
        print "pdb requested"
        ppdb.enabled = not sys.argv[0].endswith("nosetests")
        sys.argv.remove("--pdb")

    unittest.main()

call it with python myunittest.py --pdb and it will halt. Otherwise it won't.

0
-1

Some solution above modifies business logic:

try:      # <-- new code
 original_code()  # <-- changed (indented)
except Exception as e:  # <-- new code
 pdb.post_mortem(...)   # <-- new code

To minimize changes to the original code, we can define a function decorator, and simply decorate the function that's throwing:

def pm(func):
    import functools, pdb

    @functools.wraps(func)
    def func2(*args, **kwargs):
        try:
            return func(*args, **kwargs)
        except Exception as e:
            pdb.post_mortem(e.__traceback__)

   raise
    return func2

Use:

@pm
def test_xxx(...):
 ...
-1

Buildt a module with a decorator which post mortems into every type of error except AssertionError. The decorator can be triggered by the logging root level

#!/usr/bin/env python3
'''
Decorator for getting post mortem on errors of a unittest TestCase
'''
import sys
import pdb
import functools
import traceback
import logging
import unittest

logging.basicConfig(format='%(asctime)s %(message)s', level=logging.DEBUG)

def debug_on(log_level):
    '''
    Function decorator for post mortem debugging unittest functions.

    Args:
      log_level (int): logging levels coesponding to logging stl module

    Usecase:

    class tests(unittest.TestCase):
        @debug_on(logging.root.level)
        def test_trigger_pdb(self):
            assert 1 == 0
    '''

    def decorator(f):

        @functools.wraps(f)
        def wrapper(*args, **kwargs):
            try:
                return f(*args, **kwargs)
            except BaseException as err:
                info = sys.exc_info()
                traceback.print_exception(*info)

                if log_level < logging.INFO and type(err) != AssertionError:
                    pdb.post_mortem(info[2])

        return wrapper

    return decorator


class Debug_onTester(unittest.TestCase):

    @debug_on(logging.root.level)
    def test_trigger_pdb(self):
        assert 1 == 0


if __name__ == '__main__':
    unittest.main()
1
  • dif: Added the functionality that an AssertionError will behave like intendet. Cheers Aug 26, 2022 at 11:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.