Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Doc/library/unittest.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2265,6 +2265,12 @@ Loading and running tests
.. versionchanged:: 3.12
Added the *durations* keyword parameter.

.. method:: getName(test)

Return a test name used in test results.

.. versionadded:: 3.14

.. data:: defaultTestLoader

Instance of the :class:`TestLoader` class intended to be shared. If no
Expand Down
5 changes: 5 additions & 0 deletions Doc/whatsnew/3.14.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1855,6 +1855,11 @@ symtable

(Contributed by Bénédikt Tran in :gh:`120029`.)

unittest
--------

* The new :meth:`.TextTestResult.getName` method allows customizing test names
in test results.

sys
---
Expand Down
24 changes: 24 additions & 0 deletions Lib/test/test_unittest/test_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,30 @@ def testGetDescriptionWithoutDocstring(self):
'testGetDescriptionWithoutDocstring (' + __name__ +
'.Test_TextTestResult.testGetDescriptionWithoutDocstring)')

def testCustomGetNameWithoutDocstring(self):
class CustomGetNameTextTestResult(unittest.TextTestResult):
def getName(self, test):
return test.id()

result = CustomGetNameTextTestResult(None, True, 1)
expected_test_name = __name__ + ".Test_TextTestResult.testCustomGetNameWithoutDocstring"
self.assertEqual(result.getName(self), expected_test_name)
self.assertEqual(result.getDescription(self), expected_test_name)

def testCustomGetNameWithDocstring(self):
"""Test docstring."""
class CustomGetNameTextTestResult(unittest.TextTestResult):
def getName(self, test):
return test.id()

result = CustomGetNameTextTestResult(None, True, 1)
expected_test_name = __name__ + ".Test_TextTestResult.testCustomGetNameWithDocstring"
self.assertEqual(result.getName(self), expected_test_name)
self.assertEqual(
result.getDescription(self),
expected_test_name + "\nTest docstring.",
)

def testGetSubTestDescriptionWithoutDocstring(self):
with self.subTest(foo=1, bar=2):
result = unittest.TextTestResult(None, True, 1)
Expand Down
8 changes: 6 additions & 2 deletions Lib/unittest/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,14 @@ def __init__(self, stream, descriptions, verbosity, *, durations=None):

def getDescription(self, test):
doc_first_line = test.shortDescription()
test_name = self.getName(test)
if self.descriptions and doc_first_line:
return '\n'.join((str(test), doc_first_line))
return f'{test_name}\n{doc_first_line}'
else:
return str(test)
return test_name

def getName(self, test):
return str(test)

def startTest(self, test):
super(TextTestResult, self).startTest(test)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Add ``TextTestResult.getName()`` method to allow customizing test names.
Patch by Mariusz Felisiak.
Loading