Show logging output when using pytest

I'm not able to see logging output when using pytest with PyCharm.

Any suggestions how to enable it?

Thanks!

4 comments
Comment actions Permalink

A code example and steps to reproduce would be very helpful in identifying the issue

0
Comment actions Permalink

here is the test code:

 

import logging
logger = logging.getLogger(__name__)


def test_logging():
for i in range(10**2):
print('aaa')
logger.critical('bbb')
logger.debug('ccc')
assert True

 

I cannot see the print and logger output until the test is over. In practice, I need to see all of them real time

0
Comment actions Permalink

I would say a more proper-ish way would be to log into a file and then tail it. That's what logging is used for, otherwise just use prints.

import logging

logging.basicConfig(
filename='test.log',
filemode='w',
level=logging.ERROR,
format = '%(asctime)s - %(levelname)s: %(message)s',
)

def test_logging():
for i in range(10**2):
print('aaa')
logging.critical('bbb')
logging.debug('ccc')
assert True
0
Comment actions Permalink

Hi Andrey,

agree in general with your comment about logging usage, but using logging as if it is print during development is really helpful...

In fact I found out that it's a supported feature by pytest https://docs.pytest.org/en/latest/logging.html#live-logs .

 

By creating a pytest.ini file with this content:

[pytest]
log_cli = true

and adding this parameter

--log-cli-level=10

to pytest run configuration, I got what I wanted :)

0

Please sign in to leave a comment.