Commit 16ea19fc authored by Berker Peksag's avatar Berker Peksag

Issue #25651: Allow falsy values to be used for msg parameter of subTest()

parent 1ddf53d4
...@@ -17,6 +17,7 @@ from .util import (strclass, safe_repr, _count_diff_all_purpose, ...@@ -17,6 +17,7 @@ from .util import (strclass, safe_repr, _count_diff_all_purpose,
__unittest = True __unittest = True
_subtest_msg_sentinel = object()
DIFF_OMITTED = ('\nDiff is %s characters long. ' DIFF_OMITTED = ('\nDiff is %s characters long. '
'Set self.maxDiff to None to see it.') 'Set self.maxDiff to None to see it.')
...@@ -497,7 +498,7 @@ class TestCase(object): ...@@ -497,7 +498,7 @@ class TestCase(object):
result.addSuccess(test_case) result.addSuccess(test_case)
@contextlib.contextmanager @contextlib.contextmanager
def subTest(self, msg=None, **params): def subTest(self, msg=_subtest_msg_sentinel, **params):
"""Return a context manager that will return the enclosed block """Return a context manager that will return the enclosed block
of code in a subtest identified by the optional message and of code in a subtest identified by the optional message and
keyword parameters. A failure in the subtest marks the test keyword parameters. A failure in the subtest marks the test
...@@ -1397,7 +1398,7 @@ class _SubTest(TestCase): ...@@ -1397,7 +1398,7 @@ class _SubTest(TestCase):
def _subDescription(self): def _subDescription(self):
parts = [] parts = []
if self._message: if self._message is not _subtest_msg_sentinel:
parts.append("[{}]".format(self._message)) parts.append("[{}]".format(self._message))
if self.params: if self.params:
params_desc = ', '.join( params_desc = ', '.join(
......
...@@ -323,6 +323,16 @@ class Test_TestResult(unittest.TestCase): ...@@ -323,6 +323,16 @@ class Test_TestResult(unittest.TestCase):
'testGetSubTestDescriptionWithoutDocstringAndParams ' 'testGetSubTestDescriptionWithoutDocstringAndParams '
'(' + __name__ + '.Test_TestResult) (<subtest>)') '(' + __name__ + '.Test_TestResult) (<subtest>)')
def testGetSubTestDescriptionForFalsyValues(self):
expected = 'testGetSubTestDescriptionForFalsyValues (%s.Test_TestResult) [%s]'
result = unittest.TextTestResult(None, True, 1)
for arg in [0, None, []]:
with self.subTest(arg):
self.assertEqual(
result.getDescription(self._subtest),
expected % (__name__, arg)
)
def testGetNestedSubTestDescriptionWithoutDocstring(self): def testGetNestedSubTestDescriptionWithoutDocstring(self):
with self.subTest(foo=1): with self.subTest(foo=1):
with self.subTest(bar=2): with self.subTest(bar=2):
......
...@@ -77,6 +77,8 @@ Core and Builtins ...@@ -77,6 +77,8 @@ Core and Builtins
Library Library
------- -------
- Issue #25651: Allow falsy values to be used for msg parameter of subTest().
- Issue #27932: Prevent memory leak in win32_ver(). - Issue #27932: Prevent memory leak in win32_ver().
- Fix UnboundLocalError in socket._sendfile_use_sendfile. - Fix UnboundLocalError in socket._sendfile_use_sendfile.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment