How to print to console in pytest?
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.
def test_good(): for i in range(1000): print(i)def test_bad(): print('this should fail!') assert False
Results in the following output:
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 ======================py.test tmp.py============================= test session starts ==============================platform darwin -- Python
Captured stdout section.
If you would like to see
-s flag to
py.test. However, note that this can sometimes be difficult to parse.
2.7.6 -- py-1.4.20 -- pytest-2.5.2plugins: cache, cov, pep8, xdistcollected 2 itemstmp.py 0123and 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 ======================py.test tmp.py -s============================= test session starts ==============================platform darwin -- Python
From the docs:
During test execution any output sent to stdout and stderr is captured. If a test or a setup method fails its according captured output will usually be shown along with the failure traceback.
pytest has the option
--capture=method in which
method is per-test capturing method, and could be one of the following:
pytest also has the option
-s which is a shortcut for
--capture=no, and this is the option that will allow you to see your print statements in the console.
pytest --capture=no # show print statements in consolepytest -s # equivalent to previous command
Setting capturing methods or disabling capturing
There are two ways in which
pytest can perform capturing:
file descriptor (FD) level capturing (default): All writes going to the operating system file descriptors 1 and 2 will be captured.
sys level capturing: Only writes to Python files sys.stdout and sys.stderr will be captured. No capturing of writes to filedescriptors is performed.
pytest -s # disable all capturingpytest --capture=sys # replace sys.stdout/stderr with in-mem filespytest --capture=fd # also point filedescriptors 1 and 2 to temp file
-s option will print output of all functions, which may be too much.
If you need particular output, the doc page you mentioned offers few suggestions:
assert False, "dumb assert to make PyTest print my stuff"at the end of your function, and you will see your output due to failed test.
You have special object passed to you by PyTest, and you can write the output into a file to inspect it later, like
def test_good1(capsys): for i in range(5): print i out, err = capsys.readouterr() open("err.txt", "w").write(err) open("out.txt", "w").write(out)
You can open the
errfiles in a separate tab and let editor automatically refresh it for you, or do a simple
py.test; cat out.txtshell command to run your test.
That is rather hackish way to do stuff, but may be it is the stuff you need: after all, TDD means you mess with stuff and leave it clean and silent when it's ready :-).