This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: Add --debug command line option to unittest to enable post-mortem debugging
Type: enhancement Stage: patch review
Components: Versions: Python 3.10
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Dominik V., andrei.avk, gregory.p.smith, remi.lapeyre
Priority: normal Keywords: patch

Created on 2020-12-22 23:46 by Dominik V., last changed 2022-04-11 14:59 by admin.

Pull Requests
URL Status Linked Edit
PR 23900 open Dominik V., 2020-12-22 23:52
Messages (3)
msg383624 - (view) Author: Dominik Vilsmeier (Dominik V.) * Date: 2020-12-22 23:46
Currently there is no option to use post-mortem debugging via `pdb`
on a `unittest` test case which fails due to an exception being leaked.

Consider the following example:

import unittest

def foo():
    for x in [1, 2, 'oops', 4]:
        print(x + 100)

class TestFoo(unittest.TestCase):
    def test_foo(self):
        self.assertIs(foo(), None)

if __name__ == '__main__':

If we were calling `foo` directly we could enter post-mortem debugging via
`python -m pdb`.
However since `foo` is wrapped in a test case, `unittest` eats the
exception and thus prevents post-mortem debugging.

So I propose adding a
command-line option `--debug` to unittest for running
test cases in debug mode so that post-mortem debugging can be used.

I see that some third-party distributions enable this, but since both
`unittest` and `pdb` are part of the standard library, it would
be nice if they played well together.
Plus the required methods are already in place (`TestCase.debug`
and `TestSuite.debug`).

There is also a popular StackOverflow question on this topic:
msg385498 - (view) Author: Dominik Vilsmeier (Dominik V.) * Date: 2021-01-22 13:23
Is anybody interested in reviewing the PR? It seems like a useful enhancement to me.
msg393961 - (view) Author: Andrei Kulakov (andrei.avk) * (Python triager) Date: 2021-05-19 18:25
This is a duplicate of, which already has some discussion going for a few years, so please follow up there.
Date User Action Args
2022-04-11 14:59:39adminsetgithub: 86888
2021-05-19 19:09:06ned.deilysetnosy: + gregory.p.smith
2021-05-19 18:25:08andrei.avksetnosy: + andrei.avk
messages: + msg393961
2021-04-13 10:19:57remi.lapeyresetnosy: + remi.lapeyre
2021-01-22 13:23:10Dominik V.setmessages: + msg385498
versions: + Python 3.10
2020-12-22 23:52:55Dominik V.setkeywords: + patch
stage: patch review
pull_requests: + pull_request22755
2020-12-22 23:46:51Dominik V.create