By default, py.test
captures the result of standard out so that it can control how it prints it out. If it didn’t do this, it would spew out a lot of text without the context of what test printed that text.
However, if a test fails, it will include a section in the resulting report that shows what was printed to standard out in that particular test.
For example,
def test_good(): for i in range(1000): print(i) def test_bad(): print('this should fail!') assert False
Results in the following output:
>>> py.test tmp.py ============================= test session starts ============================== platform darwin -- Python 2.7.6 -- py-1.4.20 -- pytest-2.5.2 plugins: cache, cov, pep8, xdist collected 2 items tmp.py .F =================================== FAILURES =================================== ___________________________________ test_bad ___________________________________ def test_bad(): print('this should fail!') > assert False E assert False tmp.py:7: AssertionError ------------------------------- Captured stdout -------------------------------- this should fail! ====================== 1 failed, 1 passed in 0.04 seconds ======================
Note the Captured stdout
section.
If you would like to see print
statements as they are executed, you can pass the -s
flag to py.test
. However, note that this can sometimes be difficult to parse.
>>> py.test tmp.py -s ============================= test session starts ============================== platform darwin -- Python 2.7.6 -- py-1.4.20 -- pytest-2.5.2 plugins: cache, cov, pep8, xdist collected 2 items tmp.py 0 1 2 3 ... and so on ... 997 998 999 .this should fail! F =================================== FAILURES =================================== ___________________________________ test_bad ___________________________________ def test_bad(): print('this should fail!') > assert False E assert False tmp.py:7: AssertionError ====================== 1 failed, 1 passed in 0.02 seconds ======================