Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion Lib/unittest/case.py
Original file line number Diff line number Diff line change
Expand Up @@ -745,7 +745,11 @@ def __call__(self, *args, **kwds):
def debug(self):
"""Run the test without collecting errors in a TestResult"""
self.setUp()
getattr(self, self._testMethodName)()
testMethod = getattr(self, self._testMethodName)
if (not getattr(self.__class__, "__unittest_skip__", False) and

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's not necessary use the round brackets for the condition.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean the parens around the if statement? It is not necessary, but encouraged by the style guide to wrap multi-line if statements in parens: https://www.python.org/dev/peps/pep-0008/#multiline-if-statements

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it's out of the scope of this PR, but I'd like to suggest a small refactoring to make this condition a little bit clearer.

  1. Define a new _is_skipped(self, testMethod) method:
def _is_skipped(self, testMethod):
    return any(
        getattr(t, "__unittest_skip__", False)
        for t in (self.__class__, testMethod)
    )
  1. Update the new condition to not self._is_skipped(testMethod) and the existing condition around line 665 to self._is_skipped(testMethod)

If it doesn't make sense to include this small update in this PR, but you think it makes sense in general, I'd be happy to work on it.

not getattr(testMethod, "__unittest_skip__", False)):
# If the class or method was not skipped run it
testMethod()
self.tearDown()
while self._cleanups:
function, args, kwargs = self._cleanups.pop(-1)
Expand Down
25 changes: 25 additions & 0 deletions Lib/unittest/test/test_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -1777,6 +1777,31 @@ def test_something(self):
self.assertEqual(len(result.skipped), 1)
self.assertEqual(result.testsRun, 1)

def testSkippingDebugMode(self):
def _skip(self=None):
raise unittest.SkipTest('some reason')
def nothing(self):
pass

class Test1(unittest.TestCase):
test_something = _skip

class Test2(unittest.TestCase):
setUp = _skip
test_something = nothing

class Test3(unittest.TestCase):
test_something = nothing
tearDown = _skip

class Test4(unittest.TestCase):
def test_something(self):
self.addCleanup(_skip)

for klass in (Test1, Test2, Test3, Test4):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What did you mean with "klass"?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a common variable name used throughout this file (see line 1774 for example), it's a variable representing a class.

with self.assertRaises(unittest.SkipTest):
klass('test_something').debug()

def testSystemExit(self):
def _raise(self=None):
raise SystemExit
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Stops skipped tests from running with TestCase.debug(), following the
functionality of TestCase.run().