[Python-checkins] cpython (3.5): Issue #25651: Allow falsy values to be used for msg parameter of subTest()

berker.peksag python-checkins at python.org
Wed Sep 21 12:34:38 EDT 2016


https://hg.python.org/cpython/rev/ba743894e793
changeset: 103998:ba743894e793
branch: 3.5
parent: 103984:00f090563f1b
user: Berker Peksag <berker.peksag at gmail.com>
date: Wed Sep 21 19:34:15 2016 +0300
summary:
 Issue #25651: Allow falsy values to be used for msg parameter of subTest()
files:
 Lib/unittest/case.py | 5 +++--
 Lib/unittest/test/test_result.py | 10 ++++++++++
 Misc/NEWS | 2 ++
 3 files changed, 15 insertions(+), 2 deletions(-)
diff --git a/Lib/unittest/case.py b/Lib/unittest/case.py
--- a/Lib/unittest/case.py
+++ b/Lib/unittest/case.py
@@ -17,6 +17,7 @@
 
 __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 @@
 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 @@
 
 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
--- a/Lib/unittest/test/test_result.py
+++ b/Lib/unittest/test/test_result.py
@@ -323,6 +323,16 @@
 'testGetSubTestDescriptionWithoutDocstringAndParams '
 '(' + __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):
 with self.subTest(foo=1):
 with self.subTest(bar=2):
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -77,6 +77,8 @@
 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.
-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /