Python 3.4.1,pytest 2.6.2.
当测试失败时,pytest将定期报告测试中打印到stdout的内容.例如这段代码:
def method_under_test():
print("Hallo, Welt!")
return 41
def test_result_only():
result = method_under_test()
assert result == 42
Run Code Online (Sandbox Code Playgroud)
执行时python -m pytest myfile.py,将报告此:
================================== FAILURES ===================================
______________________________ test_result_only _______________________________
def test_result_only():
result = method_under_test()
> assert result == 42
E assert 41 == 42
pytestest.py:9: AssertionError
---------------------------- Captured stdout call -----------------------------
Hallo, Welt!
========================== 1 failed in 0.03 seconds ===========================
Run Code Online (Sandbox Code Playgroud)
这是一个非常好的功能.但是当我使用pytest的内置capsys夹具时,如下所示:
def test_result_and_stdout(capsys):
result = method_under_test()
out, err = capsys.readouterr()
assert out.startswith("Hello")
assert result …Run Code Online (Sandbox Code Playgroud)