python / cpython

The Python programming language
https://www.python.org
Other
63.86k stars 30.57k forks source link

unittest needs a way to launch pdb.post_mortem or other debug hooks #62965

Open gpshead opened 11 years ago

gpshead commented 11 years ago
BPO 18765
Nosy @terryjreedy, @gpshead, @voidspace, @Windsooon, @remilapeyre, @benedwards14, @Dominik1123

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields: ```python assignee = None closed_at = None created_at = labels = ['easy', '3.8', 'type-feature', 'library'] title = 'unittest needs a way to launch pdb.post_mortem or other debug hooks' updated_at = user = 'https://github.com/gpshead' ``` bugs.python.org fields: ```python activity = actor = 'Dominik V.' assignee = 'none' closed = False closed_date = None closer = None components = ['Library (Lib)'] creation = creator = 'gregory.p.smith' dependencies = [] files = [] hgrepos = [] issue_num = 18765 keywords = ['easy'] message_count = 10.0 messages = ['195444', '195519', '334851', '334868', '341846', '341859', '355474', '367872', '367873', '393984'] nosy_count = 7.0 nosy_names = ['terry.reedy', 'gregory.p.smith', 'michael.foord', 'Windson Yang', 'remi.lapeyre', 'benedwards14', 'Dominik V.'] pr_nums = [] priority = 'normal' resolution = None stage = 'needs patch' status = 'open' superseder = None type = 'enhancement' url = 'https://bugs.python.org/issue18765' versions = ['Python 3.8'] ```

gpshead commented 11 years ago

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.

voidspace commented 11 years ago

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)?

23982c60-ed6c-47d1-96c2-69d417bd81b3 commented 5 years ago

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.

23982c60-ed6c-47d1-96c2-69d417bd81b3 commented 5 years ago

@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.

3db4c488-648f-4df6-97a9-da1ac9fc355c commented 5 years ago

Hello, @Rémi, are you still working on this issue?

23982c60-ed6c-47d1-96c2-69d417bd81b3 commented 5 years ago

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 bpo-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 :)

eaad1604-de27-40aa-b44a-110887766197 commented 5 years ago

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?

terryjreedy commented 4 years ago

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.

terryjreedy commented 4 years ago

See bpo-36825 about making TestCase argument aware.

9e91563d-5324-47be-9efc-a18b9f8d164b commented 3 years ago

I wasn't aware of this issue so apparently I submitted a duplicate (bpo-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.

kxrob commented 2 years ago

PR #99169 suggests both a breaking debug mode (--debug, seamless external post-mortem handling, originally inspired by #23900) and "in-line" post-mortem and trace debug modes (--pdb --pm=... --trace). Maybe you can have a look.

achhina commented 9 months ago

Hi, friendly ping, this would be very useful to have! Is there anything else that would be needed for @kxrob's PR before being accepted?