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

时间:2012-10-18 20:01:33

标签: python unit-testing

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

<test name> ... ok 

如果测试具有

形式的文档字符串
"""
test that so and so happens
"""

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

"""test that so and so happens"""

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

<test name>
test that so and so happens ... ok

我无法找到记录此行为的位置。有没有办法解决这个问题?

2 个答案:

答案 0 :(得分:16)

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

class MyTests(unittest.TestCase):
    #  ....
    def shortDescription(self):
        return None

通过始终返回None,您可以完全关闭此功能。如果您希望以不同方式格式化文档字符串,则可以将其作为self._testMethodDoc

答案 1 :(得分:0)

读完之后,我为鼻子测试做了一个插件,以避免使用样板。

https://github.com/MarechJ/nosenodocstrings

相关问题