Skip to content

Commit d636a63

Browse files
serhiy-storchakamiss-islington
authored andcommitted
bpo-36674: Honour the skipping decorators in TestCase.debug() (pythonGH-28446)
unittest.TestCase.debug() raises now a SkipTest if the class or the test method are decorated with the skipping decorator. Previously it only raised a SkipTest if the test method was decorated with other decorator in addition to the skipping decorator, or if SkipTest was explicitly raised in the test or setup methods. (cherry picked from commit dea59cf) Co-authored-by: Serhiy Storchaka <[email protected]>
1 parent a18d522 commit d636a63

File tree

3 files changed

+77
-1
lines changed

3 files changed

+77
-1
lines changed

Lib/unittest/case.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -652,8 +652,16 @@ def __call__(self, *args, **kwds):
652652

653653
def debug(self):
654654
"""Run the test without collecting errors in a TestResult"""
655+
testMethod = getattr(self, self._testMethodName)
656+
if (getattr(self.__class__, "__unittest_skip__", False) or
657+
getattr(testMethod, "__unittest_skip__", False)):
658+
# If the class or method was skipped.
659+
skip_why = (getattr(self.__class__, '__unittest_skip_why__', '')
660+
or getattr(testMethod, '__unittest_skip_why__', ''))
661+
raise SkipTest(skip_why)
662+
655663
self.setUp()
656-
getattr(self, self._testMethodName)()
664+
testMethod()
657665
self.tearDown()
658666
while self._cleanups:
659667
function, args, kwargs = self._cleanups.pop(-1)

Lib/unittest/test/test_skipping.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,5 +460,71 @@ def test_1(self):
460460
self.assertIs(suite.run(result), result)
461461
self.assertEqual(result.skipped, [(test, "")])
462462

463+
def test_debug_skipping(self):
464+
class Foo(unittest.TestCase):
465+
def setUp(self):
466+
events.append("setUp")
467+
def tearDown(self):
468+
events.append("tearDown")
469+
def test1(self):
470+
self.skipTest('skipping exception')
471+
events.append("test1")
472+
@unittest.skip("skipping decorator")
473+
def test2(self):
474+
events.append("test2")
475+
476+
events = []
477+
test = Foo("test1")
478+
with self.assertRaises(unittest.SkipTest) as cm:
479+
test.debug()
480+
self.assertIn("skipping exception", str(cm.exception))
481+
self.assertEqual(events, ["setUp"])
482+
483+
events = []
484+
test = Foo("test2")
485+
with self.assertRaises(unittest.SkipTest) as cm:
486+
test.debug()
487+
self.assertIn("skipping decorator", str(cm.exception))
488+
self.assertEqual(events, [])
489+
490+
def test_debug_skipping_class(self):
491+
@unittest.skip("testing")
492+
class Foo(unittest.TestCase):
493+
def setUp(self):
494+
events.append("setUp")
495+
def tearDown(self):
496+
events.append("tearDown")
497+
def test(self):
498+
events.append("test")
499+
500+
events = []
501+
test = Foo("test")
502+
with self.assertRaises(unittest.SkipTest) as cm:
503+
test.debug()
504+
self.assertIn("testing", str(cm.exception))
505+
self.assertEqual(events, [])
506+
507+
def test_debug_skipping_subtests(self):
508+
class Foo(unittest.TestCase):
509+
def setUp(self):
510+
events.append("setUp")
511+
def tearDown(self):
512+
events.append("tearDown")
513+
def test(self):
514+
with self.subTest(a=1):
515+
events.append('subtest')
516+
self.skipTest("skip subtest")
517+
events.append('end subtest')
518+
events.append('end test')
519+
520+
events = []
521+
result = LoggingResult(events)
522+
test = Foo("test")
523+
with self.assertRaises(unittest.SkipTest) as cm:
524+
test.debug()
525+
self.assertIn("skip subtest", str(cm.exception))
526+
self.assertEqual(events, ['setUp', 'subtest'])
527+
528+
463529
if __name__ == "__main__":
464530
unittest.main()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
:meth:`unittest.TestCase.debug` raises now a :class:`unittest.SkipTest` if
2+
the class or the test method are decorated with the skipping decorator.

0 commit comments

Comments
 (0)