python unittest - 使用 'buffer' 选项抑制标准输出 - 我该怎么做?

标签 python unit-testing

在单元测试文档中 [ http://docs.python.org/2/library/unittest.html#unittest.main ], 我看到描述了以下方法签名:

unittest.main([module[, defaultTest[, argv[, testRunner[, testLoader[, exit[, verbosity[, failfast[, catchbreak[, buffer]]]]]]]]]])

最后一个选项是“缓冲区”。文档解释了有关此选项的以下内容:

The failfast, catchbreak and buffer parameters have the same effect as the same-name command-line options.

命令行选项的文档 [ http://docs.python.org/2/library/unittest.html#command-line-options ] 解释'buffer'如下:

-b, --buffer
The standard output and standard error streams are buffered during the test run. Output during a passing test is discarded. Output is echoed normally on test fail or error and is added to the failure messages.

我有以下演示代码,它没有表现出预期的行为:

import unittest2

class DemoTest(unittest2.TestCase):
    def test_one(self):
        self.assertTrue(True)

    def test_two(self):
        self.assertTrue(True)

if __name__ == '__main__':
    test_program = unittest2.main(verbosity=0, buffer=True, exit=False)

这个程序的输出是:

----------------------------------------------------------------------
Ran 2 tests in 0.000s

OK

事实上,如果我将程序的最后一行更改为:

test_program = unittest2.main(verbosity=0, buffer="hello", exit=False)

我做错了什么? (我尝试使用 unittest 而不是 unittest2,但没有任何区别。)

最佳答案

重点是 buffer option影响测试中的标准输出写入,忽略 unittest2 行为。也就是说,如果你添加像这样的字符串,你会看到不同之处

print "Suppress me!"

对于任何测试方法,如果您选择 buffer=False,该表达式将出现在标准输出上,而如果您将其设置为 True,它将被抑制。

关于python unittest - 使用 'buffer' 选项抑制标准输出 - 我该怎么做?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14245499/

相关文章:

python - QWebView() 的替代方法

python - REDIS:python 中的 redis 不返回任何内容

python - SQLAlchemy 中的计数和分组依据

database - 单元测试依赖于数据库的窗口服务

javascript - 使用 Jasmine/Node : Uncaught type error "Cannot set property of ' mock' undefined 测试 Angular

java - 为整个包生成 randoop 测试用例

python - 将 LinkedIn API 时间转换为 Python 中的标准日期

python - 使用networkx根据边缘的重复更新权重信息

java - 我们如何为涉及与数据库连接的方法编写单元测试?

c++ - 存储测试数据