Skip to content

Disable pytest logging plugin for single testΒ #9018

Open
@rooterkyberian

Description

@rooterkyberian

What's the problem this feature will solve?

I'm trying to test a CLI tool which produces stdout/stderr output through logging.
I need to make sure the logging is properly configured and stdout/stderr is indeed produced with proper formatting.
I don't want to check configuration, but actual output of the command while retaining ability to mock things (this is why running cli tool in subprocess call is not an option).

Describe the solution you'd like

Any solution which won't easily break with pytest upgrade and prevent pytest from messing with logging configuration will be good.
Ideally pytest would also be able to restore the logging configuration before test was started, but that seems like a another feature.

Solution ideas:

def suspend_logcapture(pytestconfig):
    manager = pytestconfig.pluginmanager
    plugin = manager.get_plugin("logging-plugin")
    manager.unregister(plugin)
    yield
    manager.register(plugin)  # currently breaks on pytest_addoption

Alternative Solutions

right now I'm using following which I consider bad since I rely on pytest internals:

@pytest.fixture
def suspend_logcapture(pytestconfig):
    from _pytest.logging import LogCaptureHandler
    @contextlib.contextmanager
    def context(*args, **kwargs):
        yield LogCaptureHandler()

    with patch("_pytest.logging.catching_logs", context):
        yield

Tried also going through temporary removing handlers & settings set by pytest

@pytest.fixture
def suspend_logcapture():
    logger = logging.getLogger()
    config = {
        "disabled": False,
        "handlers": [],
        "level": logging.NOTSET,
    }
    old_config = {attr: getattr(logger, attr) for attr in config}
    for attr, value in config.items():
        setattr(logger, attr, value)
    yield
    for attr, value in old_config.items():
        setattr(logger, attr, value)

but pytest thinks nothing of it, as somehow the logging plugin is given yet another chance to modify root logger before the actual test gets run and sees its handler was removed and readds it

Additional context

solution ideas are based on:
https://github.com/kvas-it/pytest-console-scripts
https://pypi.org/project/pytest-disable-plugin/

As to why I didn't use them - code audits are a thing and costs time; also the core the problem seemed to me like more and issue with pytest itself.

Metadata

Metadata

Assignees

No one assigned

    Labels

    plugin: capturerelated to the capture builtin pluginplugin: loggingrelated to the logging builtin plugin

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions