簡體   English   中英

pytest:獲取AttributeError:'CaptureFixture'對象沒有屬性'readouterror'捕獲標准輸出

[英]pytest: getting AttributeError: 'CaptureFixture' object has no attribute 'readouterror' capturing stdout

這應該很簡單,但是我無法弄清楚是什么導致了錯誤。

我正在關注如何將stdout捕獲到對象中的pytest文檔 ,但遇到以下錯誤:

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 --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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM