'In PyCharm using pytest integration, can print() output be suppressed for passed tests?
UPDATE: I've raised this with JetBrains. They have responded to say it's not configurable and merely a usability issue and not a bug in PyCharm. I've questioned their response. The docs linked below say "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." which to me clearly says the behaviour below is a bug.
ORGINAL: I'm trying to settle on a good workflow for unit tests in Python. I use PyCharm and pytest seems more popular and less verbose than unittest. My existing manual tests use print() for debugging and this seems useful and supported. https://docs.pytest.org/en/6.2.x/capture.html#using-print-statements-for-debugging
def test_func1():
print("debug output from test 1")
assert True
def test_func2():
print("debug output from test 2")
assert False
When I Run this in Pycharm with pytest selected as the "default test runner" I get:
simple_test.py::test_func1 PASSED [ 50%]debug output from test 1
simple_test.py::test_func2 FAILED [100%]debug output from test 2
Problem: The print statement from the passed test is appearing (also there's no newline).
When I run pytest from the Terminal with default settings I only get:
debug output from test 2
which I believe is how pytest is supposed to work and this will become crucial when I have lots of tests.
I like the integrated test UI in Pycharm especially the tree-structure and how it shows each test running, but this feels like a deal-breaker for a large project? Is there a way to get it working the way it should (only showing print output for failed tests)?
I've tried passing "additional arguments" in Run Configuration such as -s but it appears to me that PyCharm (possibly using TeamCity plugin) is generating the output rather than it coming from pytest directly so passing arguments to pytest might not help. Making PYTEST quieter when run from PYCHARM
I'm using Ubuntu 20.04.2, PyCharm 2021.1.2 (Community), Python 3.9.2, pytest-6.2.4, conda 4.10.1
Solution 1:[1]
in order to show print output in pycharm debug process please follow these steps:
1)create a pytest.ini
in your project and add this
[pytest]
log_cli = true
- Inside your debug settings (clicking in edit settings at the left of run in the IDE), in additional arguments add
-s --capture=no --log-cli-level=10
this works for me!!!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | clagccs |