Add command-line option to unittest for enabling post-mortem debugging
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__': unittest.main() If we were calling `foo` directly we could enter post-mortem debugging via `python -m pdb test.py`. However since `foo` is wrapped in a test case, `unittest` eats the exception and thus prevents post-mortem debugging. `--failfast` doesn't help, the exception is still swallowed. Since I am not aware of a solution that enables post-mortem debugging in such a case (without modifying the test scripts, please correct me if one exists), I propose adding a command-line option to `unittest` for [running test cases in debug mode](https://docs.python.org/3/library/unittest.html#unittest.TestCase.debug) so that post-mortem debugging can be used. P.S.: There is also [this SO question](https://stackoverflow.com/q/4398967/3767239) on a similar topic.
In case someone is interested, I created a corresponding pull request here: https://github.com/python/cpython/pull/23900 It's a lightweight change since the relevant methods `TestCase.debug` and `TestSuite.debug` were already in place. The docstring of these methods makes it clear that this is what they were intended to be used for: "Run the test without collecting the result. This allows exceptions raised by the test to be propagated to the caller, and can be used to support running tests under a debugger." Dominik Vilsmeier wrote:
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__': unittest.main()
If we were calling foo directly we could enter post-mortem debugging via python -m pdb test.py. However since foo is wrapped in a test case, unittest eats the exception and thus prevents post-mortem debugging. --failfast doesn't help, the exception is still swallowed. Since I am not aware of a solution that enables post-mortem debugging in such a case (without modifying the test scripts, please correct me if one exists), I propose adding a command-line option to unittest for running test cases in debug mode so that post-mortem debugging can be used. P.S.: There is also this SO question on a similar topic.
participants (1)
-
Dominik Vilsmeier