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: gregory.p.smith, michael.foord, remi.lapeyre
Priority: normal Keywords: easy

Created on 2013-08-16 23:45 by gregory.p.smith, last changed 2019-02-05 14:37 by remi.lapeyre.

Messages (4)
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.
History
Date User Action Args
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