Skip to content

Commit 150914d

Browse files
committedMay 18, 2023
caplog un-disable logging, add missing test coverage
- 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
1 parent bc5a27f commit 150914d

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed
 

‎testing/logging/test_fixture.py

+33
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,39 @@ def test_with_statement_logging_disabled(caplog, cleanup_disabled_logging):
185185
assert logging.root.manager.disable == logging.CRITICAL
186186

187187

188+
@pytest.mark.parametrize(
189+
"level_str,expected_disable_level",
190+
[
191+
("CRITICAL", logging.ERROR),
192+
("ERROR", logging.WARNING),
193+
("WARNING", logging.INFO),
194+
("INFO", logging.DEBUG),
195+
("DEBUG", logging.NOTSET),
196+
("NOTSET", logging.NOTSET),
197+
("NOTVALIDLEVEL", logging.NOTSET),
198+
],
199+
)
200+
def test_force_enable_logging_level_string(
201+
caplog, cleanup_disabled_logging, level_str, expected_disable_level
202+
):
203+
"""Test force_enable_logging using a level string.
204+
205+
``expected_disable_level`` is one level below ``level_str`` because the disabled log level
206+
always needs to be *at least* one level lower than the level that caplog is trying to capture.
207+
"""
208+
test_logger = logging.getLogger("test_str_level_force_enable")
209+
# Emulate a testing environment where all logging is disabled.
210+
logging.disable(logging.CRITICAL)
211+
# Make sure all logging is disabled.
212+
assert not test_logger.isEnabledFor(logging.CRITICAL)
213+
# Un-disable logging for `level_str`.
214+
caplog.force_enable_logging(level_str, test_logger)
215+
# Make sure that the disabled level is now one below the requested logging level.
216+
# We don't use `isEnabledFor` here because that also checks the level set by
217+
# `logging.setLevel()` which is irrelevant to `logging.disable()`.
218+
assert test_logger.manager.disable == expected_disable_level
219+
220+
188221
def test_log_access(caplog):
189222
caplog.set_level(logging.INFO)
190223
logger.info("boo %s", "arg")

0 commit comments

Comments
 (0)
Please sign in to comment.