summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2021-08-30 13:16:25 (GMT)
committerGitHub <noreply@github.com>2021-08-30 13:16:25 (GMT)
commit7e246a3a7b43762480ee4fe0cfb859e8e997a8c8 (patch)
tree03d23f0d379834e72bcc405f9ed69ea510f8a8d5
parentd6cb5dd9e19210f5963ff8beadde7ca2fda71574 (diff)
downloadcpython-7e246a3a7b43762480ee4fe0cfb859e8e997a8c8.zip
cpython-7e246a3a7b43762480ee4fe0cfb859e8e997a8c8.tar.gz
cpython-7e246a3a7b43762480ee4fe0cfb859e8e997a8c8.tar.bz2
bpo-41620: TestCase.run() now always return a TestResult instance (GH-28030)
Previously it returned None if the test class or method was decorated with a skipping decorator. Co-authored-by: Iman Tabrizian <iman.tabrizian@gmail.com>
-rw-r--r--Lib/unittest/case.py2
-rw-r--r--Lib/unittest/test/test_skipping.py53
-rw-r--r--Misc/NEWS.d/next/Library/2021-08-29-14-49-22.bpo-41620.WJ6PFL.rst3
3 files changed, 34 insertions, 24 deletions
diff --git a/Lib/unittest/case.py b/Lib/unittest/case.py
index 8775ba9..2d193eb 100644
--- a/Lib/unittest/case.py
+++ b/Lib/unittest/case.py
@@ -575,7 +575,7 @@ class TestCase(object):
skip_why = (getattr(self.__class__, '__unittest_skip_why__', '')
or getattr(testMethod, '__unittest_skip_why__', ''))
self._addSkip(result, self, skip_why)
- return
+ return result
expecting_failure = (
getattr(self, "__unittest_expecting_failure__", False) or
diff --git a/Lib/unittest/test/test_skipping.py b/Lib/unittest/test/test_skipping.py
index 3adde41..79b27e6 100644
--- a/Lib/unittest/test/test_skipping.py
+++ b/Lib/unittest/test/test_skipping.py
@@ -14,14 +14,16 @@ class Test_TestSkipping(unittest.TestCase):
events = []
result = LoggingResult(events)
test = Foo("test_skip_me")
- test.run(result)
+ self.assertIs(test.run(result), result)
self.assertEqual(events, ['startTest', 'addSkip', 'stopTest'])
self.assertEqual(result.skipped, [(test, "skip")])
events = []
- test.run()
+ result = test.run()
self.assertEqual(events, ['startTestRun', 'startTest', 'addSkip',
'stopTest', 'stopTestRun'])
+ self.assertEqual(result.skipped, [(test, "skip")])
+ self.assertEqual(result.testsRun, 1)
# Try letting setUp skip the test now.
class Foo(unittest.TestCase):
@@ -33,15 +35,17 @@ class Test_TestSkipping(unittest.TestCase):
events = []
result = LoggingResult(events)
test = Foo("test_nothing")
- test.run(result)
+ self.assertIs(test.run(result), result)
self.assertEqual(events, ['startTest', 'addSkip', 'stopTest'])
self.assertEqual(result.skipped, [(test, "testing")])
self.assertEqual(result.testsRun, 1)
events = []
- test.run()
+ result = test.run()
self.assertEqual(events, ['startTestRun', 'startTest', 'addSkip',
'stopTest', 'stopTestRun'])
+ self.assertEqual(result.skipped, [(test, "testing")])
+ self.assertEqual(result.testsRun, 1)
def test_skipping_subtests(self):
class Foo(unittest.TestCase):
@@ -56,7 +60,7 @@ class Test_TestSkipping(unittest.TestCase):
events = []
result = LoggingResult(events)
test = Foo("test_skip_me")
- test.run(result)
+ self.assertIs(test.run(result), result)
self.assertEqual(events, ['startTest', 'addSkip', 'addSkip',
'addSkip', 'stopTest'])
self.assertEqual(len(result.skipped), 3)
@@ -71,10 +75,12 @@ class Test_TestSkipping(unittest.TestCase):
self.assertEqual(result.skipped[2], (test, "skip 3"))
events = []
- test.run()
+ result = test.run()
self.assertEqual(events,
['startTestRun', 'startTest', 'addSkip', 'addSkip',
'addSkip', 'stopTest', 'stopTestRun'])
+ self.assertEqual([msg for subtest, msg in result.skipped],
+ ['skip 1', 'skip 2', 'skip 3'])
def test_skipping_decorators(self):
op_table = ((unittest.skipUnless, False, True),
@@ -95,7 +101,7 @@ class Test_TestSkipping(unittest.TestCase):
suite = unittest.TestSuite([test_do_skip, test_dont_skip])
events = []
result = LoggingResult(events)
- suite.run(result)
+ self.assertIs(suite.run(result), result)
self.assertEqual(len(result.skipped), 1)
expected = ['startTest', 'addSkip', 'stopTest',
'startTest', 'addSuccess', 'stopTest']
@@ -105,16 +111,16 @@ class Test_TestSkipping(unittest.TestCase):
self.assertTrue(result.wasSuccessful())
events = []
- test_do_skip.run()
- self.assertEqual(len(result.skipped), 1)
+ result = test_do_skip.run()
self.assertEqual(events, ['startTestRun', 'startTest', 'addSkip',
'stopTest', 'stopTestRun'])
+ self.assertEqual(result.skipped, [(test_do_skip, "testing")])
events = []
- test_dont_skip.run()
- self.assertEqual(len(result.skipped), 1)
+ result = test_dont_skip.run()
self.assertEqual(events, ['startTestRun', 'startTest', 'addSuccess',
'stopTest', 'stopTestRun'])
+ self.assertEqual(result.skipped, [])
def test_skip_class(self):
@unittest.skip("testing")
@@ -128,15 +134,16 @@ class Test_TestSkipping(unittest.TestCase):
result = LoggingResult(events)
test = Foo("test_1")
suite = unittest.TestSuite([test])
- suite.run(result)
+ self.assertIs(suite.run(result), result)
self.assertEqual(events, ['startTest', 'addSkip', 'stopTest'])
self.assertEqual(result.skipped, [(test, "testing")])
self.assertEqual(record, [])
events = []
- test.run()
+ result = test.run()
self.assertEqual(events, ['startTestRun', 'startTest', 'addSkip',
'stopTest', 'stopTestRun'])
+ self.assertEqual(result.skipped, [(test, "testing")])
self.assertEqual(record, [])
def test_skip_non_unittest_class(self):
@@ -150,7 +157,7 @@ class Test_TestSkipping(unittest.TestCase):
result = unittest.TestResult()
test = Foo("test_1")
suite = unittest.TestSuite([test])
- suite.run(result)
+ self.assertIs(suite.run(result), result)
self.assertEqual(result.skipped, [(test, "testing")])
self.assertEqual(record, [])
@@ -162,7 +169,7 @@ class Test_TestSkipping(unittest.TestCase):
events = []
result = LoggingResult(events)
test = Foo("test_die")
- test.run(result)
+ self.assertIs(test.run(result), result)
self.assertEqual(events,
['startTest', 'addExpectedFailure', 'stopTest'])
self.assertEqual(result.expectedFailures[0][0], test)
@@ -177,7 +184,7 @@ class Test_TestSkipping(unittest.TestCase):
events = []
result = LoggingResult(events)
test = Foo("test_1")
- test.run(result)
+ self.assertIs(test.run(result), result)
self.assertEqual(events,
['startTest', 'addExpectedFailure', 'stopTest'])
self.assertEqual(result.expectedFailures[0][0], test)
@@ -195,7 +202,7 @@ class Test_TestSkipping(unittest.TestCase):
events = []
result = LoggingResult(events)
test = Bar("test_1")
- test.run(result)
+ self.assertIs(test.run(result), result)
self.assertEqual(events,
['startTest', 'addExpectedFailure', 'stopTest'])
self.assertEqual(result.expectedFailures[0][0], test)
@@ -218,7 +225,7 @@ class Test_TestSkipping(unittest.TestCase):
events = []
result = LoggingResult(events)
test = Foo("test_die")
- test.run(result)
+ self.assertIs(test.run(result), result)
self.assertEqual(events,
['startTest', 'addSubTestSuccess',
'addExpectedFailure', 'stopTest'])
@@ -234,7 +241,7 @@ class Test_TestSkipping(unittest.TestCase):
events = []
result = LoggingResult(events)
test = Foo("test_die")
- test.run(result)
+ self.assertIs(test.run(result), result)
self.assertEqual(events,
['startTest', 'addUnexpectedSuccess', 'stopTest'])
self.assertFalse(result.failures)
@@ -256,7 +263,7 @@ class Test_TestSkipping(unittest.TestCase):
events = []
result = LoggingResult(events)
test = Foo("test_die")
- test.run(result)
+ self.assertIs(test.run(result), result)
self.assertEqual(events,
['startTest',
'addSubTestSuccess', 'addSubTestSuccess',
@@ -280,7 +287,7 @@ class Test_TestSkipping(unittest.TestCase):
result = unittest.TestResult()
test = Foo("test_1")
suite = unittest.TestSuite([test])
- suite.run(result)
+ self.assertIs(suite.run(result), result)
self.assertEqual(result.skipped, [(test, "testing")])
self.assertFalse(Foo.wasSetUp)
self.assertFalse(Foo.wasTornDown)
@@ -300,7 +307,7 @@ class Test_TestSkipping(unittest.TestCase):
result = unittest.TestResult()
test = Foo("test_1")
suite = unittest.TestSuite([test])
- suite.run(result)
+ self.assertIs(suite.run(result), result)
self.assertEqual(result.skipped, [(test, "testing")])
def test_skip_without_reason(self):
@@ -312,7 +319,7 @@ class Test_TestSkipping(unittest.TestCase):
result = unittest.TestResult()
test = Foo("test_1")
suite = unittest.TestSuite([test])
- suite.run(result)
+ self.assertIs(suite.run(result), result)
self.assertEqual(result.skipped, [(test, "")])
if __name__ == "__main__":
diff --git a/Misc/NEWS.d/next/Library/2021-08-29-14-49-22.bpo-41620.WJ6PFL.rst b/Misc/NEWS.d/next/Library/2021-08-29-14-49-22.bpo-41620.WJ6PFL.rst
new file mode 100644
index 0000000..7674d4c
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2021-08-29-14-49-22.bpo-41620.WJ6PFL.rst
@@ -0,0 +1,3 @@
+:meth:`~unittest.TestCase.run` now always return a
+:class:`~unittest.TestResult` instance. Previously it returned ``None`` if
+the test class or method was decorated with a skipping decorator.