【问题标题】:Writing a pytest function for checking the output on console (stdout)编写一个 pytest 函数来检查控制台上的输出(stdout)
【发布时间】:2013-12-28 17:52:53
【问题描述】:

This link 描述了如何使用 pytest 来捕获控制台输出。 我尝试了以下简单代码,但出现错误

import sys
import pytest
def f(name):
    print "hello "+ name

def test_add(capsys):
    f("Tom")
    out,err=capsys.readouterr()
    assert out=="hello Tom"


test_add(sys.stdout)

输出:

python test_pytest.py 
hello Tom
Traceback (most recent call last):
  File "test_pytest.py", line 12, in <module>
    test_add(sys.stdout)
  File "test_pytest.py", line 8, in test_add
    out,err=capsys.readouterr()
AttributeError: 'file' object has no attribute 'readouterr'

出了什么问题,需要什么修复?谢谢

编辑: 根据评论,我更改了capfd,但我仍然得到同样的错误

import sys
import pytest
def f(name):
    print "hello "+ name

def test_add(capfd):
    f("Tom")
    out,err=capfd.readouterr()
    assert out=="hello Tom"


test_add(sys.stdout)

【问题讨论】:

    标签: python printing stdout pytest


    【解决方案1】:

    使用capfd 夹具。

    示例:

    def test_foo(capfd):
        foo()  # Writes "Hello World!" to stdout
        out, err = capfd.readouterr()
        assert out == "Hello World!"
    

    更多详情请见:http://pytest.org/en/latest/fixture.html

    请参阅:py.test --fixtures 以获取内置灯具列表。

    您的示例存在一些问题。这是一个更正的版本:

    def f(name):
        print "hello {}".format(name)
    
    
    def test_f(capfd):
        f("Tom")
    
        out, err = capfd.readouterr()
        assert out == "hello Tom\n"
    

    注意:

    • 不要使用 sys.stdout -- 使用 pytest 提供的 capfd 固定装置。
    • 使用以下命令运行测试:py.test foo.py

    测试运行输出:

    $ py.test foo.py
    ====================================================================== test session starts ======================================================================
    platform linux2 -- Python 2.7.5 -- pytest-2.4.2
    plugins: flakes, cache, pep8, cov
    collected 1 items 
    
    foo.py .
    
    =================================================================== 1 passed in 0.01 seconds ====================================================================
    

    另请注意:

    • 您不需要在您的测试模块中运行您的测试函数py.testCLI 工具和测试运行器)为您完成这项工作。

    py.test 主要做了三件事:

    1. 收集您的测试
    2. 运行测试
    3. 显示统计信息和可能的错误

    默认情况下,py.test 在您的测试模块中查找(可配置的 iirctest_foo.py 测试模块和test_foo() 测试函数。

    【讨论】:

    • 这没有帮助。我收到相同的错误消息。我粘贴了上面的代码供您参考
    • 你用的是什么版本的pytest?尝试升级。这按预期工作,并在我的所有单元测试中显示。
    • 你从不在代码中调用这个函数test_f(capfd),可以吗?
    • 它如何知道要运行哪个函数进行测试? test_xxx 的所有函数?
    • 正确。这是可配置的 IRRC,但默认情况下会在这些测试模块中查找 test_foo.py 模块和 test_foo() 函数。
    【解决方案2】:

    问题在于您在第一个代码 sn-p 块的最后显式调用测试函数:

    test_add(sys.stdout)
    

    你不应该这样做;调用你的测试函数是 pytest 的工作。 当它这样做时,它将识别名称capsys(或capfd,就此而言) 并自动为您提供合适的 pytest 内部对象作为调用参数。 (example given in the pytest documentation 已经很完整了。)

    该对象将提供所需的readouterr() 函数。 sys.stdout 没有那个功能,这就是你的程序失败的原因。

    【讨论】:

      猜你喜欢
      • 2023-03-08
      • 2011-04-25
      • 2016-12-03
      • 2023-02-02
      • 2014-01-29
      • 2020-04-09
      • 2023-03-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多