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!

5
9 comments

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

0

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

3

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
-6

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

7

> 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

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.

3

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

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

5

I'm usign the pytest.ini file to configure the live log.

Unfortunatly with live log enable, pycharm get confused and is not able to understand properly the test results into the tree:

For example: I run 13 tests into a package and all of the always succeds. Without live log enable Pycharm correctly report: "Tests passed: 13 of 13 tests". With live log enable, Pycharm report is unpredittable.

This is without live log enable

This with livelog enable:

I have added the white area just to suppress sensitive data, sorry.

Can you fix this bug?

1

Please sign in to leave a comment.