mirror of
https://github.com/python/cpython.git
synced 2025-11-01 22:21:35 +00:00
bpo-30664: The description of a unittest subtest now preserves the (#2265)
order of keyword arguments of TestCase.subTest().
This commit is contained in:
parent
c38e32a100
commit
48fbe52ac7
3 changed files with 29 additions and 7 deletions
|
|
@ -338,6 +338,16 @@ def __exit__(self, exc_type, exc_value, tb):
|
||||||
.format(logging.getLevelName(self.level), self.logger.name))
|
.format(logging.getLevelName(self.level), self.logger.name))
|
||||||
|
|
||||||
|
|
||||||
|
class _OrderedChainMap(collections.ChainMap):
|
||||||
|
def __iter__(self):
|
||||||
|
seen = set()
|
||||||
|
for mapping in self.maps:
|
||||||
|
for k in mapping:
|
||||||
|
if k not in seen:
|
||||||
|
seen.add(k)
|
||||||
|
yield k
|
||||||
|
|
||||||
|
|
||||||
class TestCase(object):
|
class TestCase(object):
|
||||||
"""A class whose instances are single test cases.
|
"""A class whose instances are single test cases.
|
||||||
|
|
||||||
|
|
@ -514,7 +524,7 @@ def subTest(self, msg=_subtest_msg_sentinel, **params):
|
||||||
return
|
return
|
||||||
parent = self._subtest
|
parent = self._subtest
|
||||||
if parent is None:
|
if parent is None:
|
||||||
params_map = collections.ChainMap(params)
|
params_map = _OrderedChainMap(params)
|
||||||
else:
|
else:
|
||||||
params_map = parent.params.new_child(params)
|
params_map = parent.params.new_child(params)
|
||||||
self._subtest = _SubTest(self, msg, params_map)
|
self._subtest = _SubTest(self, msg, params_map)
|
||||||
|
|
@ -1418,7 +1428,7 @@ def _subDescription(self):
|
||||||
if self.params:
|
if self.params:
|
||||||
params_desc = ', '.join(
|
params_desc = ', '.join(
|
||||||
"{}={!r}".format(k, v)
|
"{}={!r}".format(k, v)
|
||||||
for (k, v) in sorted(self.params.items()))
|
for (k, v) in self.params.items())
|
||||||
parts.append("({})".format(params_desc))
|
parts.append("({})".format(params_desc))
|
||||||
return " ".join(parts) or '(<subtest>)'
|
return " ".join(parts) or '(<subtest>)'
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -307,7 +307,7 @@ def testGetSubTestDescriptionWithoutDocstring(self):
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
result.getDescription(self._subtest),
|
result.getDescription(self._subtest),
|
||||||
'testGetSubTestDescriptionWithoutDocstring (' + __name__ +
|
'testGetSubTestDescriptionWithoutDocstring (' + __name__ +
|
||||||
'.Test_TestResult) (bar=2, foo=1)')
|
'.Test_TestResult) (foo=1, bar=2)')
|
||||||
with self.subTest('some message'):
|
with self.subTest('some message'):
|
||||||
result = unittest.TextTestResult(None, True, 1)
|
result = unittest.TextTestResult(None, True, 1)
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
|
|
@ -335,12 +335,21 @@ def testGetSubTestDescriptionForFalsyValues(self):
|
||||||
|
|
||||||
def testGetNestedSubTestDescriptionWithoutDocstring(self):
|
def testGetNestedSubTestDescriptionWithoutDocstring(self):
|
||||||
with self.subTest(foo=1):
|
with self.subTest(foo=1):
|
||||||
with self.subTest(bar=2):
|
with self.subTest(baz=2, bar=3):
|
||||||
result = unittest.TextTestResult(None, True, 1)
|
result = unittest.TextTestResult(None, True, 1)
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
result.getDescription(self._subtest),
|
result.getDescription(self._subtest),
|
||||||
'testGetNestedSubTestDescriptionWithoutDocstring '
|
'testGetNestedSubTestDescriptionWithoutDocstring '
|
||||||
'(' + __name__ + '.Test_TestResult) (bar=2, foo=1)')
|
'(' + __name__ + '.Test_TestResult) (baz=2, bar=3, foo=1)')
|
||||||
|
|
||||||
|
def testGetDuplicatedNestedSubTestDescriptionWithoutDocstring(self):
|
||||||
|
with self.subTest(foo=1, bar=2):
|
||||||
|
with self.subTest(baz=3, bar=4):
|
||||||
|
result = unittest.TextTestResult(None, True, 1)
|
||||||
|
self.assertEqual(
|
||||||
|
result.getDescription(self._subtest),
|
||||||
|
'testGetDuplicatedNestedSubTestDescriptionWithoutDocstring '
|
||||||
|
'(' + __name__ + '.Test_TestResult) (baz=3, bar=4, foo=1)')
|
||||||
|
|
||||||
@unittest.skipIf(sys.flags.optimize >= 2,
|
@unittest.skipIf(sys.flags.optimize >= 2,
|
||||||
"Docstrings are omitted with -O2 and above")
|
"Docstrings are omitted with -O2 and above")
|
||||||
|
|
@ -362,7 +371,7 @@ def testGetSubTestDescriptionWithOneLineDocstring(self):
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
result.getDescription(self._subtest),
|
result.getDescription(self._subtest),
|
||||||
('testGetSubTestDescriptionWithOneLineDocstring '
|
('testGetSubTestDescriptionWithOneLineDocstring '
|
||||||
'(' + __name__ + '.Test_TestResult) (bar=2, foo=1)\n'
|
'(' + __name__ + '.Test_TestResult) (foo=1, bar=2)\n'
|
||||||
'Tests getDescription() for a method with a docstring.'))
|
'Tests getDescription() for a method with a docstring.'))
|
||||||
|
|
||||||
@unittest.skipIf(sys.flags.optimize >= 2,
|
@unittest.skipIf(sys.flags.optimize >= 2,
|
||||||
|
|
@ -390,7 +399,7 @@ def testGetSubTestDescriptionWithMultiLineDocstring(self):
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
result.getDescription(self._subtest),
|
result.getDescription(self._subtest),
|
||||||
('testGetSubTestDescriptionWithMultiLineDocstring '
|
('testGetSubTestDescriptionWithMultiLineDocstring '
|
||||||
'(' + __name__ + '.Test_TestResult) (bar=2, foo=1)\n'
|
'(' + __name__ + '.Test_TestResult) (foo=1, bar=2)\n'
|
||||||
'Tests getDescription() for a method with a longer '
|
'Tests getDescription() for a method with a longer '
|
||||||
'docstring.'))
|
'docstring.'))
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -374,6 +374,9 @@ Extension Modules
|
||||||
Library
|
Library
|
||||||
-------
|
-------
|
||||||
|
|
||||||
|
- bpo-30664: The description of a unittest subtest now preserves the order of
|
||||||
|
keyword arguments of TestCase.subTest().
|
||||||
|
|
||||||
- [Security] bpo-30730: Prevent environment variables injection in subprocess on
|
- [Security] bpo-30730: Prevent environment variables injection in subprocess on
|
||||||
Windows. Prevent passing other environment variables and command arguments.
|
Windows. Prevent passing other environment variables and command arguments.
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue