Skip to content

Commit

Permalink
caplog un-disable logging, add missing test coverage
Browse files Browse the repository at this point in the history
- Adds test coverage for `force_enable_logging` with a string `level`. Fixes [code coverage check](https://github.com/pytest-dev/pytest/pull/8758/checks?check_run_id=4123920877)

Issue: pytest-dev#8711
PR: pytest-dev#8758
  • Loading branch information
alexlambson committed Nov 11, 2021
1 parent fe822d9 commit e52ee7c
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions testing/logging/test_fixture.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,39 @@ def test_with_statement_logging_disabled(caplog, cleanup_disabled_logging):
assert logging.root.manager.disable == logging.CRITICAL


@pytest.mark.parametrize(
"level_str,expected_disable_level",
[
("CRITICAL", logging.ERROR),
("ERROR", logging.WARNING),
("WARNING", logging.INFO),
("INFO", logging.DEBUG),
("DEBUG", logging.NOTSET),
("NOTSET", logging.NOTSET),
("NOTVALIDLEVEL", logging.NOTSET),
],
)
def test_force_enable_logging_level_string(
caplog, cleanup_disabled_logging, level_str, expected_disable_level
):
"""Test force_enable_logging using a level string.
``expected_disable_level`` is one level below ``level_str`` because the disabled log level
always needs to be *at least* one level lower than the level that caplog is trying to capture.
"""
test_logger = logging.getLogger("test_str_level_force_enable")
# Emulate a testing environment where all logging is disabled.
logging.disable(logging.CRITICAL)
# Make sure all logging is disabled.
assert not test_logger.isEnabledFor(logging.CRITICAL)
# Un-disable logging for `level_str`.
caplog.force_enable_logging(level_str, test_logger)
# Make sure that the disabled level is now one below the requested logging level.
# We don't use `isEnabledFor` here because that also checks the level set by
# `logging.setLevel()` which is irrelevant to `logging.disable()`.
assert test_logger.manager.disable == expected_disable_level


def test_log_access(caplog):
caplog.set_level(logging.INFO)
logger.info("boo %s", "arg")
Expand Down

0 comments on commit e52ee7c

Please sign in to comment.