Skip to content

caplog.set_level() doesn't override log_level #7133

@ruaridhw

Description

@ruaridhw

A "global" log level set by log_level cannot be overridden at the test level.

Minimal Example:

# setup.cfg
[tool:pytest]
log_level = CRITICAL

# test_example.py
import logging


def function_that_logs():
    logging.debug('message')


def test_logger(caplog):
    caplog.set_level(logging.DEBUG)
    function_that_logs()
    assert caplog.text

Running pytest-master@077d1c3, Python 3.7.6.

It looks like this is because log_level adjusts the level of LogCaptureHandler() in _runtest_for_main() whereas caplog.set_level() adjusts the level of a specific Logger (likely Root).

If I add self.handler.setLevel(level) to the end of LogCaptureFixture.set_level() the above example passes. Is this a suitable fix (assuming the corresponding cleanup is added too)? Or is this a misuse of log_level?

Metadata

Metadata

Assignees

No one assigned

    Labels

    plugin: loggingrelated to the logging builtin plugintype: enhancementnew feature or API change, should be merged into features branch

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions