From 377d826e5b6043632549ee95335594247b2fa033 Mon Sep 17 00:00:00 2001 From: Alex Lambson Date: Wed, 10 Nov 2021 22:38:18 -0700 Subject: [PATCH] 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: https://github.com/pytest-dev/pytest/issues/8711 PR: https://github.com/pytest-dev/pytest/pull/8758 --- testing/logging/test_fixture.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/testing/logging/test_fixture.py b/testing/logging/test_fixture.py index 94656da28d..07fe722a7d 100644 --- a/testing/logging/test_fixture.py +++ b/testing/logging/test_fixture.py @@ -185,6 +185,36 @@ 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 enabling logging using a level string. + + The disabled log level always needs to be one level lower than the level + that we want caplog to catch, so the expected level will be one below the desired level string. + """ + 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 was disabled. + assert not test_logger.isEnabledFor(logging.CRITICAL) + caplog.force_enable_logging(level_str, test_logger) + # Make sure that the disabled level is now one below the requested logging level. + assert test_logger.manager.disable == expected_disable_level + + def test_log_access(caplog): caplog.set_level(logging.INFO) logger.info("boo %s", "arg")