mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
Fix bug in TestResult.addSubTest()
This commit is contained in:
parent
e612c1e29f
commit
22e162f462
2 changed files with 35 additions and 1 deletions
|
@ -134,7 +134,7 @@ class TestResult(object):
|
||||||
errors = self.failures
|
errors = self.failures
|
||||||
else:
|
else:
|
||||||
errors = self.errors
|
errors = self.errors
|
||||||
errors.append((test, self._exc_info_to_string(err, test)))
|
errors.append((subtest, self._exc_info_to_string(err, test)))
|
||||||
self._mirrorOutput = True
|
self._mirrorOutput = True
|
||||||
|
|
||||||
def addSuccess(self, test):
|
def addSuccess(self, test):
|
||||||
|
|
|
@ -227,6 +227,40 @@ class Test_TestResult(unittest.TestCase):
|
||||||
self.assertTrue(test_case is test)
|
self.assertTrue(test_case is test)
|
||||||
self.assertIsInstance(formatted_exc, str)
|
self.assertIsInstance(formatted_exc, str)
|
||||||
|
|
||||||
|
def test_addSubTest(self):
|
||||||
|
class Foo(unittest.TestCase):
|
||||||
|
def test_1(self):
|
||||||
|
nonlocal subtest
|
||||||
|
with self.subTest(foo=1):
|
||||||
|
subtest = self._subtest
|
||||||
|
try:
|
||||||
|
1/0
|
||||||
|
except ZeroDivisionError:
|
||||||
|
exc_info_tuple = sys.exc_info()
|
||||||
|
# Register an error by hand (to check the API)
|
||||||
|
result.addSubTest(test, subtest, exc_info_tuple)
|
||||||
|
# Now trigger a failure
|
||||||
|
self.fail("some recognizable failure")
|
||||||
|
|
||||||
|
subtest = None
|
||||||
|
test = Foo('test_1')
|
||||||
|
result = unittest.TestResult()
|
||||||
|
|
||||||
|
test.run(result)
|
||||||
|
|
||||||
|
self.assertFalse(result.wasSuccessful())
|
||||||
|
self.assertEqual(len(result.errors), 1)
|
||||||
|
self.assertEqual(len(result.failures), 1)
|
||||||
|
self.assertEqual(result.testsRun, 1)
|
||||||
|
self.assertEqual(result.shouldStop, False)
|
||||||
|
|
||||||
|
test_case, formatted_exc = result.errors[0]
|
||||||
|
self.assertIs(test_case, subtest)
|
||||||
|
self.assertIn("ZeroDivisionError", formatted_exc)
|
||||||
|
test_case, formatted_exc = result.failures[0]
|
||||||
|
self.assertIs(test_case, subtest)
|
||||||
|
self.assertIn("some recognizable failure", formatted_exc)
|
||||||
|
|
||||||
def testGetDescriptionWithoutDocstring(self):
|
def testGetDescriptionWithoutDocstring(self):
|
||||||
result = unittest.TextTestResult(None, True, 1)
|
result = unittest.TextTestResult(None, True, 1)
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue