如何阻止 Python unittest 打印测试文档字符串? [英] How to stop Python unittest from printing test docstring?

查看:34
本文介绍了如何阻止 Python unittest 打印测试文档字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've noticed that, when my Python unit tests contain documentation at the top of the function, sometimes the framework prints them in the test output. Normally, the test output contains one test per line:

<test name> ... ok 

如果测试有格式的文档字符串

If the test has a docstring of the form

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

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

than all is well. But if the test has a docstring all on one line:

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

然后测试输出需要不止一行,并且包含这样的文档:

then the test output takes more than one line and includes the doc like this:

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

我找不到记录在案的行为的位置.有没有办法关闭它?

I can't find where this is documented behavior. Is there a way to turn this off?

推荐答案

使用文档字符串的第一行;负责的方法是 TestCase.shortDescription(),您可以在测试用例中覆盖它:

The first line of the docstring is used; the responsible method is TestCase.shortDescription(), which you can override in your testcases:

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

总是返回None,你就完全关闭了这个功能.如果你想以不同的方式格式化文档字符串,它可以作为 self._testMethodDoc 使用.

By always returning None you turn the feature off entirely. If you want to format the docstring differently, it's available as self._testMethodDoc.

这篇关于如何阻止 Python unittest 打印测试文档字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆