gpt4 book ai didi

python - pytest:获取 AttributeError: 'CaptureFixture' 对象没有属性 'readouterror' 捕获标准输出

转载 作者:太空宇宙 更新时间:2023-11-03 20:30:40 28 4
gpt4 key购买 nike

这应该是一件简单的事情,但我无法弄清楚是什么导致了错误。

我正在关注 pytest docs关于如何将标准输出捕获到对象中,但出现以下错误:

capsys = <_pytest.capture.CaptureFixture object at 0x7f02a1e2f7f0>

def test_can_output_to_stdout(capsys):
print("hello")
> capture = capsys.readouterror()
E AttributeError: 'CaptureFixture' object has no attribute 'readouterror'

test_aaa.py:5: AttributeError

我使用的代码类似于:

import pytest

def test_can_output_to_stdout(capsys):
print("hello")
capture = capsys.readouterror()
assert "hello" in capture.out

我这样调用测试:

py.test --capture=sys --capture=fd test_aaa.py

版本是:

py测试:

py.test --version
This is pytest version 4.6.5, imported from /usr/local/lib/python3.4/site-packages/pytest.p

Python:

python --version
Python 3.4.8

任何帮助将不胜感激。

最佳答案

事实证明,复制粘贴可能比写出示例更好。错误在于属性名称。

它应该是capsys.readouterr(),而不是capsys.readouterror(),并且完整:

import pytest

def test_can_output_to_stdout(capsys):
print("hello")
capture = capsys.readouterr()
assert "hello" in capture.out

关于python - pytest:获取 AttributeError: 'CaptureFixture' 对象没有属性 'readouterror' 捕获标准输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57535901/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com