【问题标题】:pytest: getting AttributeError: 'CaptureFixture' object has no attribute 'readouterror' capturing stdoutpytest:获取 AttributeError:“CaptureFixture”对象没有属性“readouterror”捕获标准输出
【发布时间】:2019-08-17 11:33:47
【问题描述】:

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

我正在关注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

版本是:

pytest:

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

任何帮助将不胜感激。

【问题讨论】:

    标签: python python-3.x pytest stdio


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 2019-06-03
      • 2015-02-17
      • 2020-12-18
      • 2013-11-06
      • 2021-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多