Skip to content

Commit b3d4e7f

Browse files
committed
Issue python#25651: Merge from 3.5
2 parents 349f552 + 16ea19f commit b3d4e7f

3 files changed

Lines changed: 15 additions & 2 deletions

File tree

‎Lib/unittest/case.py‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
__unittest = True
1919

20+
_subtest_msg_sentinel = object()
2021

2122
DIFF_OMITTED = ('\nDiff is %s characters long. '
2223
'Set self.maxDiff to None to see it.')
@@ -497,7 +498,7 @@ def _addSkip(self, result, test_case, reason):
497498
result.addSuccess(test_case)
498499

499500
@contextlib.contextmanager
500-
def subTest(self, msg=None, **params):
501+
def subTest(self, msg=_subtest_msg_sentinel, **params):
501502
"""Return a context manager that will return the enclosed block
502503
of code in a subtest identified by the optional message and
503504
keyword parameters. A failure in the subtest marks the test
@@ -1397,7 +1398,7 @@ def runTest(self):
13971398

13981399
def _subDescription(self):
13991400
parts = []
1400-
if self._message:
1401+
if self._message is not _subtest_msg_sentinel:
14011402
parts.append("[{}]".format(self._message))
14021403
if self.params:
14031404
params_desc = ', '.join(

‎Lib/unittest/test/test_result.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,16 @@ def testGetSubTestDescriptionWithoutDocstringAndParams(self):
323323
'testGetSubTestDescriptionWithoutDocstringAndParams '
324324
'(' + __name__ + '.Test_TestResult) (<subtest>)')
325325

326+
def testGetSubTestDescriptionForFalsyValues(self):
327+
expected = 'testGetSubTestDescriptionForFalsyValues (%s.Test_TestResult) [%s]'
328+
result = unittest.TextTestResult(None, True, 1)
329+
for arg in [0, None, []]:
330+
with self.subTest(arg):
331+
self.assertEqual(
332+
result.getDescription(self._subtest),
333+
expected % (__name__, arg)
334+
)
335+
326336
def testGetNestedSubTestDescriptionWithoutDocstring(self):
327337
with self.subTest(foo=1):
328338
with self.subTest(bar=2):

‎Misc/NEWS‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ Core and Builtins
3535
Library
3636
-------
3737

38+
- Issue #25651: Allow falsy values to be used for msg parameter of subTest().
39+
3840
- Issue #27778: Fix a memory leak in os.getrandom() when the getrandom() is
3941
interrupted by a signal and a signal handler raises a Python exception.
4042

0 commit comments

Comments
 (0)