I want to test messages logged by some functions (logging
modules) using the caplog
fixtures.
However, for some strange reasons, these logged messages keep on displaying on the console (even if explicitely set log-cli
to False or with a higher level).
Here is a some reprodcing example:
import logging
LOGGER = logging.getLogger(__name__)
def some_function():
LOGGER.info('Some function called')
LOGGER.warning('Watch out!')
def test_some_function(caplog):
some_function()
assert 'Some function called' not in caplog.text
assert 'Watch out!' in caplog.text
And this what I see in the console
PS D:\_PLAYGROUND_\TCP> pytest -p no:dash -p no:pylama -p no:allure-pytest
================================================= test session starts =================================================
platform win32 -- Python 3.9.10, pytest-7.3.1, pluggy-1.0.0
rootdir: D:\_PLAYGROUND_\TCP
configfile: pytest.ini
plugins: allure-pytest-2.12.0, azurepipelines-1.0.4, bdd-6.1.1, cov-4.0.0, html-3.2.0, instafail-0.4.2, metadata-1.11.0, mock-3.10.0, nunit-1.0.1, xdist-3.1.0
collected 1 item
test_log.py WARNING:test_log:Watch out!
. [100%]##vso[results.publish type=NUnit;runTitle='Pytest results';publishRunAttachments=true;]D:\_PLAYGROUND_\TCP\test-output.xml
##vso[task.logissue type=warning;]Coverage XML was not created, skipping upload.
----------------------- generated Nunit xml file: D:\_PLAYGROUND_\TCP\test-output.xml ------------------------
================================================== 1 passed in 0.03s ==================================================
I don't want to see the Watch out!
that messes up everything?
Any idea of what could be the problem?