如何阻止Python unittest打印测试文档字符串?

Dav*_*rns 13 python unit-testing

我注意到,当我的Python单元测试包含函数顶部的文档时,框架有时会在测试输出中打印它们.通常,测试输出每行包含一个测试:

<test name> ... ok 
Run Code Online (Sandbox Code Playgroud)

如果测试具有表单的文档字符串

"""
test that so and so happens
"""
Run Code Online (Sandbox Code Playgroud)

比一切都好.但是如果测试在一行上都有一个文档字符串:

"""test that so and so happens"""
Run Code Online (Sandbox Code Playgroud)

然后测试输出需要多行,并包含如下文档:

<test name>
test that so and so happens ... ok
Run Code Online (Sandbox Code Playgroud)

我无法找到这是记录在案的行为.有没有办法把它关掉?

Mar*_*ers 18

使用docstring 的第一行; 负责任的方法是TestCase.shortDescription(),你可以在你的测试用例中覆盖:

class MyTests(unittest.TestCase):
    #  ....
    def shortDescription(self):
        return None
Run Code Online (Sandbox Code Playgroud)

通过始终返回None您完全关闭该功能.如果要以不同方式格式化文档字符串,则可以使用它self._testMethodDoc.