Pytest print debugging not fit-for-purpose?

According to the docs https://docs.pytest.org/en/6.2.x/capture.html#using-print-statements-for-debugging using print statements for debugging is a key benefit of using pytest.

However when running from within PyCharm print statements appear mixed in with the test results and appear for tests that pass. This is different to the pytest default behaviour. I've not been able to use Additional Arguments to get the default behaviour.

def test_func1():
print("debug output from test 1")
assert True


def test_func2():
print("debug output from test 2")
assert False

Run from PyCharm produces:

simple_test.py::test_func1 PASSED                                        [ 50%]debug output from test 1

simple_test.py::test_func2 FAILED                                        [100%]debug output from test 2

pytest run from Terminal produces:

debug output from test 2

Is there a way to get PyCharm to output the print statements for failed tests only?

I think once I have a large number of tests containing lots of print call then having the passing tests always printing and doing so interleaved with the test results is going to be a deal-breaker for using pytest as an integrated tool, which is a shame since I like the PyCharm UI a lot.

I'm using Ubuntu 20.04.2, PyCharm 2021.1.2 (Community), Python 3.9.2, pytest-6.2.4, conda 4.10.1

5 comments
Comment actions Permalink

Hi, unfortunately this behavior doesn't seem to be configurable, but I wouldn't say it's a bug either (except for the broken formatting). Feel free to submit a usability issue to https://youtrack.jetbrains.com

0
Comment actions Permalink

Andrey, The link I provided to the pytest documentation says:

"One primary benefit of the default capturing of stdout/stderr output is that you can use print statements for debugging and running this module will show you precisely the output of the failing function and hide the other one."

In light of that I don't know how one can conclude that this is anything other than a bug in PyCharm's integration with pytest.

Suppose you have 100 unit pytests and you have followed pytest's recommendation and used print statements for debugging so each of your 100 statements calls print a number of times. You then run the unit tests and 99 pass and 1 fails. In order to fix the problem, do you want to see the handful of debugging statements from the 1 failure or the thousand lines of output from all 100?

PyCharm is the best Python IDE imho and pytest currently seems to be the leading test runner. If the two don't work together properly it's going to cause problems for a lot of users, especially if you don't classify it as a bug.

0
Comment actions Permalink

Robin Carter

Unfortunately, I'm not aware if this behavior was designed intentionally or by oversight. The latter would sure make it a bug. Anyway, submitting the bug report / usability issue to https://youtrack.jetbrains.com is the best option to escalate it further.

0
Comment actions Permalink

I went ahead and submitted the report https://youtrack.jetbrains.com/issue/PY-49674

Feel free to support with vote/comments.

0
Comment actions Permalink

Thanks Andrey that's very clear and good to use the code directly from the docs. I've upvoted and added a supporting comment.

0

Please sign in to leave a comment.