默认情况下,
py.test捕获标准输出的结果,以便它可以控制其输出结果的方式。如果不这样做,它将喷出大量文本,而没有测试打印该文本的上下文。
但是,如果测试失败,它将在结果报告中包括一部分,以显示在该特定测试中打印出的标准内容。
例如,
def test_good(): for i in range(1000): print(i)def test_bad(): print('this should fail!') assert False结果如下:
>>> py.test tmp.py============================= test session starts ==============================platform darwin -- Python 2.7.6 -- py-1.4.20 -- pytest-2.5.2plugins: cache, cov, pep8, xdistcollected 2 itemstmp.py .F=================================== FAILURES ===================================___________________________________ test_bad ___________________________________ def test_bad(): print('this should fail!')> assert FalseE assert Falsetmp.py:7: AssertionError------------------------------- Captured stdout --------------------------------this should fail!====================== 1 failed, 1 passed in 0.04 seconds ======================注意该
Captured stdout部分。
如果您希望
-s标志传递给
py.test。但是,请注意,有时可能难以解析。
>>> py.test tmp.py -s============================= test session starts ==============================platform darwin -- Python 2.7.6 -- py-1.4.20 -- pytest-2.5.2plugins: cache, cov, pep8, xdistcollected 2 itemstmp.py 0123... and so on ...997998999.this should fail!F=================================== FAILURES ===================================___________________________________ test_bad ___________________________________ def test_bad(): print('this should fail!')> assert FalseE assert Falsetmp.py:7: AssertionError====================== 1 failed, 1 passed in 0.02 seconds ======================


