Using pytest to test logged messages and avoid displaying logged messages on the console

163 views Asked by At

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?

1

There are 1 answers

0
Jean-Francois T. On

The first thing you should try when facing this kind of issues is to work on a clean environment (think Virtual Environment with venv).

  1. Call python -m venv .venv
  2. Activate your virtual environment (e.g. on Windows: call .venv/Scripts/activate.bat)
  3. Install pytest with pip install pytest
  4. Try again to run the tests

If the problem does not show, then you can try to investigate which of the pytest plugins would call this (by installing them one by one and try again).

In this particular case, the culprit seems to be the module pytest-azurepipelines.

Try to disable it (or uninstall it if you don't need it), and you should be good.

This is what I have got after installing all pytest pluggins except pytest-azurepipelines:

(.venv) D:\_PLAYGROUND_\TCP>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.13.2, bdd-6.1.1, cov-4.0.0, html-3.2.0, instafail-0.5.0, metadata-2.0.4, mock-3.10.0, nunit-1.0.3, xdist-3.2.1
collected 1 item

test_log.py .                                                                                                    [100%]

================================================== 1 passed in 0.04s ==================================================