Merge pull request #7571 from bluetech/logging-setlevel-handler-restore
logging: fix capture handler level not reset on teardown after caplog.set_level()
This commit is contained in:
@@ -2,6 +2,7 @@ import logging
|
||||
|
||||
import pytest
|
||||
from _pytest.logging import caplog_records_key
|
||||
from _pytest.pytester import Testdir
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
sublogger = logging.getLogger(__name__ + ".baz")
|
||||
@@ -27,8 +28,11 @@ def test_change_level(caplog):
|
||||
assert "CRITICAL" in caplog.text
|
||||
|
||||
|
||||
def test_change_level_undo(testdir):
|
||||
"""Ensure that 'set_level' is undone after the end of the test"""
|
||||
def test_change_level_undo(testdir: Testdir) -> None:
|
||||
"""Ensure that 'set_level' is undone after the end of the test.
|
||||
|
||||
Tests the logging output themselves (affacted both by logger and handler levels).
|
||||
"""
|
||||
testdir.makepyfile(
|
||||
"""
|
||||
import logging
|
||||
@@ -50,6 +54,33 @@ def test_change_level_undo(testdir):
|
||||
result.stdout.no_fnmatch_line("*log from test2*")
|
||||
|
||||
|
||||
def test_change_level_undos_handler_level(testdir: Testdir) -> None:
|
||||
"""Ensure that 'set_level' is undone after the end of the test (handler).
|
||||
|
||||
Issue #7569. Tests the handler level specifically.
|
||||
"""
|
||||
testdir.makepyfile(
|
||||
"""
|
||||
import logging
|
||||
|
||||
def test1(caplog):
|
||||
assert caplog.handler.level == 0
|
||||
caplog.set_level(41)
|
||||
assert caplog.handler.level == 41
|
||||
|
||||
def test2(caplog):
|
||||
assert caplog.handler.level == 0
|
||||
|
||||
def test3(caplog):
|
||||
assert caplog.handler.level == 0
|
||||
caplog.set_level(43)
|
||||
assert caplog.handler.level == 43
|
||||
"""
|
||||
)
|
||||
result = testdir.runpytest()
|
||||
result.assert_outcomes(passed=3)
|
||||
|
||||
|
||||
def test_with_statement(caplog):
|
||||
with caplog.at_level(logging.INFO):
|
||||
logger.debug("handler DEBUG level")
|
||||
|
||||
Reference in New Issue
Block a user