Skip to content

Commit c923c0a

Browse files
committed
gh-60492: Allow customizing test names in TextTestResult.
Thanks Chris Jerdonek for the implementation idea.
1 parent eebea7e commit c923c0a

File tree

4 files changed

+38
-2
lines changed

4 files changed

+38
-2
lines changed

Doc/whatsnew/3.13.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,12 @@ unicodedata
674674
* The Unicode database has been updated to version 15.1.0. (Contributed by
675675
James Gerity in :gh:`109559`.)
676676

677+
unittest
678+
--------
679+
680+
* The new ``TextTestResult.getName()`` method allows customizing test names in
681+
test results.
682+
677683
venv
678684
----
679685

Lib/test/test_unittest/test_result.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,30 @@ def testGetDescriptionWithoutDocstring(self):
467467
'testGetDescriptionWithoutDocstring (' + __name__ +
468468
'.Test_TextTestResult.testGetDescriptionWithoutDocstring)')
469469

470+
def testCustomGetNameWithoutDocstring(self):
471+
class CustomGetNameTextTestResult(unittest.TextTestResult):
472+
def getName(self, test):
473+
return test.id()
474+
475+
result = CustomGetNameTextTestResult(None, True, 1)
476+
expected_test_name = __name__ + ".Test_TextTestResult.testCustomGetNameWithoutDocstring"
477+
self.assertEqual(result.getName(self), expected_test_name)
478+
self.assertEqual(result.getDescription(self), expected_test_name)
479+
480+
def testCustomGetNameWithDocstring(self):
481+
"""Test docstring."""
482+
class CustomGetNameTextTestResult(unittest.TextTestResult):
483+
def getName(self, test):
484+
return test.id()
485+
486+
result = CustomGetNameTextTestResult(None, True, 1)
487+
expected_test_name = __name__ + ".Test_TextTestResult.testCustomGetNameWithDocstring"
488+
self.assertEqual(result.getName(self), expected_test_name)
489+
self.assertEqual(
490+
result.getDescription(self),
491+
expected_test_name + "\nTest docstring.",
492+
)
493+
470494
def testGetSubTestDescriptionWithoutDocstring(self):
471495
with self.subTest(foo=1, bar=2):
472496
result = unittest.TextTestResult(None, True, 1)

Lib/unittest/runner.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,14 @@ def __init__(self, stream, descriptions, verbosity, *, durations=None):
4848

4949
def getDescription(self, test):
5050
doc_first_line = test.shortDescription()
51+
test_name = self.getName(test)
5152
if self.descriptions and doc_first_line:
52-
return '\n'.join((str(test), doc_first_line))
53+
return f'{test_name}\n{doc_first_line}'
5354
else:
54-
return str(test)
55+
return test_name
56+
57+
def getName(self, test):
58+
return str(test)
5559

5660
def startTest(self, test):
5761
super(TextTestResult, self).startTest(test)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Add ``TextTestResult.getName()`` method to allow customizing test names.
2+
Patch by Mariusz Felisiak.

0 commit comments

Comments
 (0)