Skip to content

bpo-36674: Honour the skipping decorators in TestCase.debug() #28446

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
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
10 changes: 9 additions & 1 deletion Lib/unittest/case.py
Original file line number Diff line number Diff line change
Expand Up @@ -653,8 +653,16 @@ def __call__(self, *args, **kwds):

def debug(self):
"""Run the test without collecting errors in a TestResult"""
testMethod = getattr(self, self._testMethodName)
if (getattr(self.__class__, "__unittest_skip__", False) or
getattr(testMethod, "__unittest_skip__", False)):
# If the class or method was skipped.
skip_why = (getattr(self.__class__, '__unittest_skip_why__', '')
or getattr(testMethod, '__unittest_skip_why__', ''))
raise SkipTest(skip_why)

self.setUp()
getattr(self, self._testMethodName)()
testMethod()
self.tearDown()
while self._cleanups:
function, args, kwargs = self._cleanups.pop(-1)
Expand Down
66 changes: 66 additions & 0 deletions Lib/unittest/test/test_skipping.py
Original file line number Diff line number Diff line change
Expand Up @@ -460,5 +460,71 @@ def test_1(self):
self.assertIs(suite.run(result), result)
self.assertEqual(result.skipped, [(test, "")])

def test_debug_skipping(self):
class Foo(unittest.TestCase):
def setUp(self):
events.append("setUp")
def tearDown(self):
events.append("tearDown")
def test1(self):
self.skipTest('skipping exception')
events.append("test1")
@unittest.skip("skipping decorator")
def test2(self):
events.append("test2")

events = []
test = Foo("test1")
with self.assertRaises(unittest.SkipTest) as cm:
test.debug()
self.assertIn("skipping exception", str(cm.exception))
self.assertEqual(events, ["setUp"])

events = []
test = Foo("test2")
with self.assertRaises(unittest.SkipTest) as cm:
test.debug()
self.assertIn("skipping decorator", str(cm.exception))
self.assertEqual(events, [])

def test_debug_skipping_class(self):
@unittest.skip("testing")
class Foo(unittest.TestCase):
def setUp(self):
events.append("setUp")
def tearDown(self):
events.append("tearDown")
def test(self):
events.append("test")

events = []
test = Foo("test")
with self.assertRaises(unittest.SkipTest) as cm:
test.debug()
self.assertIn("testing", str(cm.exception))
self.assertEqual(events, [])

def test_debug_skipping_subtests(self):
class Foo(unittest.TestCase):
def setUp(self):
events.append("setUp")
def tearDown(self):
events.append("tearDown")
def test(self):
with self.subTest(a=1):
events.append('subtest')
self.skipTest("skip subtest")
events.append('end subtest')
events.append('end test')

events = []
result = LoggingResult(events)
test = Foo("test")
with self.assertRaises(unittest.SkipTest) as cm:
test.debug()
self.assertIn("skip subtest", str(cm.exception))
self.assertEqual(events, ['setUp', 'subtest'])


if __name__ == "__main__":
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:meth:`unittest.TestCase.debug` raises now a :class:`unittest.SkipTest` if
the class or the test method are decorated with the skipping decorator.