From: Berker Peksag Date: Wed, 21 Sep 2016 16:34:15 +0000 (+0300) Subject: Issue #25651: Allow falsy values to be used for msg parameter of subTest() X-Git-Tag: v3.6.0b2~144^2 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=16ea19fc6653ee4ec1be7cd0206073962119ac08;p=thirdparty%2FPython%2Fcpython.git Issue #25651: Allow falsy values to be used for msg parameter of subTest() --- diff --git a/Lib/unittest/case.py b/Lib/unittest/case.py index 1e4090c8facf..b523f73ff227 100644 --- a/Lib/unittest/case.py +++ b/Lib/unittest/case.py @@ -17,6 +17,7 @@ from .util import (strclass, safe_repr, _count_diff_all_purpose, __unittest = True +_subtest_msg_sentinel = object() DIFF_OMITTED = ('\nDiff is %s characters long. ' 'Set self.maxDiff to None to see it.') @@ -497,7 +498,7 @@ class TestCase(object): result.addSuccess(test_case) @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 of code in a subtest identified by the optional message and keyword parameters. A failure in the subtest marks the test @@ -1397,7 +1398,7 @@ class _SubTest(TestCase): def _subDescription(self): parts = [] - if self._message: + if self._message is not _subtest_msg_sentinel: parts.append("[{}]".format(self._message)) if self.params: params_desc = ', '.join( diff --git a/Lib/unittest/test/test_result.py b/Lib/unittest/test/test_result.py index e39e2eaecafd..0a6155356373 100644 --- a/Lib/unittest/test/test_result.py +++ b/Lib/unittest/test/test_result.py @@ -323,6 +323,16 @@ class Test_TestResult(unittest.TestCase): 'testGetSubTestDescriptionWithoutDocstringAndParams ' '(' + __name__ + '.Test_TestResult) ()') + 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): with self.subTest(foo=1): with self.subTest(bar=2): diff --git a/Misc/NEWS b/Misc/NEWS index f5efee1d1adb..e6970a45798a 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -77,6 +77,8 @@ Core and Builtins Library ------- +- Issue #25651: Allow falsy values to be used for msg parameter of subTest(). + - Issue #27932: Prevent memory leak in win32_ver(). - Fix UnboundLocalError in socket._sendfile_use_sendfile.