【发布时间】: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