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!

8 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

2
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
-3
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 :)

3
Comment actions Permalink

> I would say a more proper-ish way would be to log into a file and then tail it

No, it wouldn't

Because logging may be setted up differently in test and production environments

0
Comment actions Permalink

Pytest captures your output and your logging to display it only when your test fails. It's not a bug, it's a feature (although an unwanted one as far as I'm concerned)

You can disable the stdout/stderr capture with `-s` and disable the logs capture with `-p no:logging`. Then you will see the test output and the test logs in real time.

2
Comment actions Permalink

You can disable the stdout/stderr capture with `-s`

How?

I am trying to avoid running from command line, which is why I am using Pycharm.

 

How to do this with pycharm?

Also, how to allow all pipes to be dumped to console? I see stderr doesn't work when only the .ini file is added.

 

0
Comment actions Permalink

Noamzilo  You can add `-p no:logging -s` to "Additional Arguments" in your Run Configuration or Run Configuration Template for pytest.

5

Please sign in to leave a comment.