Python unittest,如何在报告中显示TestCase的方法的文档字符串?

Python unittest, how to display TestCase's methods' docstring in report?

题目是self-explaining。 有没有办法

class Foo(TestCase):
  def test_bar(self):
    """
    a docstring
    """

使 UnitTest 报告显示整个文档字符串?

我读到文档字符串的第一行会被打印出来(所以写

""" a docstring """

没有换行符也行)

我已阅读问题:How to stop Python unittest from printing test docstring?

但是,这是关于删除文档字符串,我不知道如何覆盖 shortDescription() 以显示完整的文档字符串。

(我没试过这个)

the answer to the SO-linked question 中说

the responsible method is TestCase.shortDescription(), which you can override in your testcases.

source of shortDescription如下:

doc = self._testMethodDoc 
return doc and doc.split("\n")[0].strip() or None

因此您可以将第二行修改为

return doc

屈服

class MyTests(unittest.TestCase):
    #  ....
    def shortDescription(self):
        doc = self._testMethodDoc 
        return doc

如果您反对使用未记录的 self._testMethodDoc,它是 testMethod.__doc__ 的直接副本,它是在 TestCase.__init__() 中通过

创建的
testMethod = getattr(self, methodName)

TestCase 的变量很少。改名的可能性很小,但你可能会问作者(也来自来源):

 47  __author__ = "Steve Purcell" 
 48  __email__ = "stephen_purcell at yahoo dot com"