Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I see normal print output created during pytest run?

Sometimes I want to just insert some print statements in my code, and see what gets printed out when I exercise it. My usual way to "exercise" it is with existing pytest tests. But when I run these, I don't seem able to see any standard output (at least from within PyCharm, my IDE).

Is there a simple way to see standard output during a pytest run?

like image 836
Des Avatar asked Jan 18 '13 18:01

Des


People also ask

Does pytest show print statements?

By default, pytest captures the output to sys. stdout/stderr and redirects it to a temporary file. This is great to get a clean and understandable test summary, but rather bad when you want to debug your code with print() statements.

What happens when you run pytest?

pytest enables you to create marks, or custom labels, for any test you like. A test may have multiple labels, and you can use them for granular control over which tests to run. Later in this tutorial, you'll see an example of how pytest marks work and learn how to make use of them in a large test suite.

What is verbose in pytest?

Verbosity. The -v flag controls the verbosity of pytest output in various aspects: test session progress, assertion details when tests fail, fixtures details with --fixtures , etc.


2 Answers

The -s switch disables per-test capturing (only if a test fails).

-s is equivalent to --capture=no.

like image 94
hpk42 Avatar answered Nov 19 '22 21:11

hpk42


pytest captures the stdout from individual tests and displays them only on certain conditions, along with the summary of the tests it prints by default.

Extra summary info can be shown using the '-r' option:

pytest -rP 

shows the captured output of passed tests.

pytest -rx 

shows the captured output of failed tests (default behaviour).

The formatting of the output is prettier with -r than with -s.

like image 33
Sunthar Avatar answered Nov 19 '22 23:11

Sunthar