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.

classification
Title: unittest needs a way to launch pdb.post_mortem or other debug hooks
Type: enhancement Stage: needs patch
Components: Library (Lib) Versions: Python 3.8
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Dominik V., Windson Yang, benedwards14, gregory.p.smith, michael.foord, remi.lapeyre, terry.reedy
Priority: normal Keywords: easy

Created on 2013-08-16 23:45 by gregory.p.smith, last changed 2022-04-11 14:57 by admin.

Messages (10)
msg195444 - (view) Author: Gregory P. Smith (gregory.p.smith) * (Python committer) Date: 2013-08-16 23:45
A few times now I've seen people write something that overrides and re-implements the unittest.TestCase run() method, copying most of the implementation but adding one feature:

The ability for pdb.post_mortem() to be called after every phase of execution iff an exception/error was caught (setUp(), the testMethod() call itself and tearDown()).

I really hate seeing people have to copy the run method implementation and modify it.

We could support this either by adding another debugHook() method to the TestCase API.  Proposal: it gets called within the except: clause surrounding each phase of test execution.

Turning on support for automatic pdb.post_mortem(), typically controlled via a flag or other environment configurable as it isn't desirable during automation:

  def debugHook(self):
    if SOMETHING_SAYS_TO_ENABLE_THIS:
      pdb.post_mortem()

no more copy and pasting the run() method.
msg195519 - (view) Author: Michael Foord (michael.foord) * (Python committer) Date: 2013-08-17 20:56
This is done in outcome.testPartExecutor. If you add it in the except clause then it is *only* called on test failure or error. If we call it unconditionally with the result (maybe a sys.exc_info tuple?) then it could have extended use cases (perhaps custom reporting)?
msg334851 - (view) Author: Rémi Lapeyre (remi.lapeyre) * Date: 2019-02-04 23:01
Hi, this is the only reason I still use pytest, I would love to be able to use only unittest and the constant copy-paste is not a great solution and is not very user friendly.

I would like to wotk on this issue.

StackOverflow has many example on how to override testRunner or to create decorators to call pm() on failure but there is no clean solution to do this yet.

It's especially a problem when using a project developped by another team that did not set up such utilities, but a default debugHook that calls pgb if a flag is given would be very useful.
msg334868 - (view) Author: Rémi Lapeyre (remi.lapeyre) * Date: 2019-02-05 14:37
@gregory.p.smith Do you think the debugHook should be added to the test case or the test runner?

If we add it to the test case, how do you think the runner should tell the test case what flags have been given on the command line?

I feel like adding a new class variable to TestCase could break some user code but no other option seems better to me.
msg341846 - (view) Author: Windson Yang (Windson Yang) * Date: 2019-05-08 02:27
Hello, @Rémi, are you still working on this issue?
msg341859 - (view) Author: Rémi Lapeyre (remi.lapeyre) * Date: 2019-05-08 08:33
Hi Windson, I had a working patch but got stuck on making this behavior optional (the `if SOMETHING_SAYS_TO_ENABLE_THIS:` in @gregory.p.smith initial post).

To make TestCase aware of the command line arguments given to the test runner I opened issue 36825 so we could do `python test_myprogram.py` but did not get any feedback on it yet.

If you see a way around this, please go ahead :)
msg355474 - (view) Author: Benjamin Edwards (benedwards14) * Date: 2019-10-27 15:56
Hi Rémi, I'd also be happy to take a look at your problem, if it's still open, do you have patch of what you've done so far?
msg367872 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2020-05-01 18:38
I would be more interested in being able to launch IDLE's visual debugger (likely after some revision) so I would want the hook to be general (as suggested by the current title) and not limited to only pdb.
msg367873 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2020-05-01 18:39
See #36825 about making TextCase argument aware.
msg393984 - (view) Author: Dominik Vilsmeier (Dominik V.) * Date: 2021-05-19 22:57
I wasn't aware of this issue so apparently I submitted a duplicate (#42722) half a year ago. The solution I implemented relies on simply calling `TestCase.debug`
and `TestSuite.debug` which makes it a lightweight change. From the perspective of the debugger the situation is thus no different than running a "normal" script. The other issue also has a pull request attached.
History
Date User Action Args
2022-04-11 14:57:49adminsetgithub: 62965
2021-05-19 22:57:42Dominik V.setnosy: + Dominik V.
messages: + msg393984
2020-05-01 18:39:28terry.reedysetmessages: + msg367873
2020-05-01 18:38:05terry.reedysetnosy: + terry.reedy
messages: + msg367872
2019-10-27 15:56:48benedwards14setnosy: + benedwards14
messages: + msg355474
2019-05-08 08:33:35remi.lapeyresetmessages: + msg341859
2019-05-08 02:27:29Windson Yangsetnosy: + Windson Yang
messages: + msg341846
2019-02-05 14:37:20remi.lapeyresetmessages: + msg334868
2019-02-05 01:27:30gregory.p.smithsetkeywords: + easy
versions: + Python 3.8, - Python 3.4
2019-02-04 23:01:31remi.lapeyresetnosy: + remi.lapeyre
messages: + msg334851
2013-08-17 20:56:43michael.foordsetmessages: + msg195519
2013-08-16 23:45:52gregory.p.smithcreate