Skip to content

Calls to logging.xxx() in skipif causes all logging calls to be duplicated to stderr #6240

@felixn

Description

@felixn

Using pytest 5.2.2 / 5.3.0, when a function called from a @pytest.mark.skipif uses a logging function, the captured log calls are duplicated to stderr.

Minimal working example:

import logging

import pytest


def _check_cond():
    logging.warning("_check_cond")
    return True


@pytest.mark.skipif(not _check_cond(), reason="_check_cond not met")
def test_logging():
    logging.warning("Schmift")

    assert False

Results in the following. Notice "Schmift" is printed both to "Captured stderr call" and "Captured log call".

$ pytest test_logging.py
======================================= test session starts ========================================
platform linux -- Python 3.7.5, pytest-5.3.0, py-1.8.0, pluggy-0.13.1
rootdir: /home/felix/src/pytest
collected 1 item                                                                                   

test_logging.py F                                                                            [100%]

============================================= FAILURES =============================================
___________________________________________ test_logging ___________________________________________

    @pytest.mark.skipif(not _check_cond(), reason="_check_cond not met")
    def test_logging():
        logging.warning("Schmift")
    
>       assert False
E       assert False

test_logging.py:15: AssertionError
--------------------------------------- Captured stderr call ---------------------------------------
WARNING:root:Schmift
---------------------------------------- Captured log call -----------------------------------------
WARNING  root:test_logging.py:13 Schmift
======================================== 1 failed in 0.03s ========================================

Removing the logging call from _check_cond() results in the expected behaviour, "Schmift" is not duplicated to stderr:

import logging

import pytest


def _check_cond():
    # logging.warning("_check_cond")
    return True


@pytest.mark.skipif(not _check_cond(), reason="_check_cond not met")
def test_logging():
    logging.warning("Schmift")

    assert False
$ pytest test_logging.py
======================================= test session starts ========================================
platform linux -- Python 3.7.5, pytest-5.3.0, py-1.8.0, pluggy-0.13.1
rootdir: /home/felix/src/pytest
collected 1 item                                                                                   

test_logging.py F                                                                            [100%]

============================================= FAILURES =============================================
___________________________________________ test_logging ___________________________________________

    @pytest.mark.skipif(not _check_cond(), reason="_check_cond not met")
    def test_logging():
        logging.warning("Schmift")
    
>       assert False
E       assert False

test_logging.py:15: AssertionError
---------------------------------------- Captured log call -----------------------------------------
WARNING  root:test_logging.py:13 Schmift
======================================== 1 failed in 0.03s =========================================

Metadata

Metadata

Assignees

No one assigned

    Labels

    plugin: loggingrelated to the logging builtin plugintopic: marksrelated to marks, either the general marks or builtintype: bugproblem that needs to be addressed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions