我正在使用Pytest 3.1.3,它应该为https://docs.pytest.org/en/latest/capture.html中描述的disabled()
装置使用capsys
方法。我正在尝试在以下简化测试中使用它:
import pytest
def test_capsys(capsys):
with capsys.disabled():
print("foo")
print("bar")
out, err = capsys.readouterr()
assert out == "bar\n"
if __name__ == "__main__":
pytest.main([__file__])
然而,显然我的断言不正确:
=================================== FAILURES ===================================
_________________________________ test_capsys __________________________________
capsys = <_pytest.capture.CaptureFixture object at 0x102741ef0>
def test_capsys(capsys):
with capsys.disabled():
print("foo")
print("bar")
out, err = capsys.readouterr()
> assert out == "bar\n"
E AssertionError: assert '' == 'bar\n'
E + bar
try_capsys.py:8: AssertionError
----------------------------- Captured stdout call -----------------------------
bar
=========================== 1 failed in 0.03 seconds ===========================
请注意,如果我注释掉with
块,则测试通过。我不明白:with
块不应该对capsys.readouterr()
没有影响吗?
更新
此问题似乎与此问题中描述的相同:https://github.com/pytest-dev/pytest/issues/1993。