-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
unittest: do not use TestCase.debug() with --pdb
#5996
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
``--trace`` now works with unittests. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Fix interaction with ``--pdb`` and unittests: do not use unittest's ``TestCase.debug()``. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
""" discovery and running of std-library "unittest" style tests. """ | ||
import functools | ||
import sys | ||
import traceback | ||
|
||
|
@@ -110,12 +111,15 @@ class TestCaseFunction(Function): | |
_testcase = None | ||
|
||
def setup(self): | ||
self._needs_explicit_tearDown = False | ||
self._testcase = self.parent.obj(self.name) | ||
self._obj = getattr(self._testcase, self.name) | ||
if hasattr(self, "_request"): | ||
self._request._fillfixtures() | ||
|
||
def teardown(self): | ||
if self._needs_explicit_tearDown: | ||
self._testcase.tearDown() | ||
self._testcase = None | ||
self._obj = None | ||
|
||
|
@@ -188,30 +192,46 @@ def addSuccess(self, testcase): | |
def stopTest(self, testcase): | ||
pass | ||
|
||
def _handle_skip(self): | ||
# implements the skipping machinery (see #2137) | ||
# analog to pythons Lib/unittest/case.py:run | ||
test_method = getattr(self._testcase, self._testcase._testMethodName) | ||
if getattr(self._testcase.__class__, "__unittest_skip__", False) or getattr( | ||
test_method, "__unittest_skip__", False | ||
): | ||
# If the class or method was skipped. | ||
skip_why = getattr( | ||
self._testcase.__class__, "__unittest_skip_why__", "" | ||
) or getattr(test_method, "__unittest_skip_why__", "") | ||
self._testcase._addSkip(self, self._testcase, skip_why) | ||
return True | ||
return False | ||
def _expecting_failure(self, test_method) -> bool: | ||
"""Return True if the given unittest method (or the entire class) is marked | ||
with @expectedFailure""" | ||
expecting_failure_method = getattr( | ||
test_method, "__unittest_expecting_failure__", False | ||
) | ||
expecting_failure_class = getattr(self, "__unittest_expecting_failure__", False) | ||
return bool(expecting_failure_class or expecting_failure_method) | ||
|
||
def runtest(self): | ||
if self.config.pluginmanager.get_plugin("pdbinvoke") is None: | ||
# TODO: move testcase reporter into separate class, this shouldnt be on item | ||
# TODO: move testcase reporter into separate class, this shouldnt be on item | ||
import unittest | ||
|
||
testMethod = getattr(self._testcase, self._testcase._testMethodName) | ||
|
||
class _GetOutOf_testPartExecutor(KeyboardInterrupt): | ||
"""Helper exception to get out of unittests's testPartExecutor (see TestCase.run).""" | ||
|
||
@functools.wraps(testMethod) | ||
def wrapped_testMethod(*args, **kwargs): | ||
"""Wrap the original method to call into pytest's machinery, so other pytest | ||
features can have a chance to kick in (notably --pdb)""" | ||
try: | ||
self.ihook.pytest_pyfunc_call(pyfuncitem=self) | ||
except unittest.SkipTest: | ||
raise | ||
except Exception as exc: | ||
expecting_failure = self._expecting_failure(testMethod) | ||
if expecting_failure: | ||
raise | ||
self._needs_explicit_tearDown = True | ||
raise _GetOutOf_testPartExecutor(exc) | ||
|
||
setattr(self._testcase, self._testcase._testMethodName, wrapped_testMethod) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does it pass #5996 (comment) then? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep! 👍 |
||
try: | ||
self._testcase(result=self) | ||
else: | ||
# disables tearDown and cleanups for post mortem debugging (see #1890) | ||
if self._handle_skip(): | ||
return | ||
self._testcase.debug() | ||
except _GetOutOf_testPartExecutor as exc: | ||
raise exc.args[0] from exc.args[0] | ||
finally: | ||
delattr(self._testcase, self._testcase._testMethodName) | ||
|
||
def _prunetraceback(self, excinfo): | ||
Function._prunetraceback(self, excinfo) | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍