2
2
3
3
import pytest
4
4
from _pytest .logging import caplog_records_key
5
+ from _pytest .pytester import Testdir
5
6
6
7
logger = logging .getLogger (__name__ )
7
8
sublogger = logging .getLogger (__name__ + ".baz" )
@@ -27,8 +28,11 @@ def test_change_level(caplog):
27
28
assert "CRITICAL" in caplog .text
28
29
29
30
30
- def test_change_level_undo (testdir ):
31
- """Ensure that 'set_level' is undone after the end of the test"""
31
+ def test_change_level_undo (testdir : Testdir ) -> None :
32
+ """Ensure that 'set_level' is undone after the end of the test.
33
+
34
+ Tests the logging output themselves (affacted both by logger and handler levels).
35
+ """
32
36
testdir .makepyfile (
33
37
"""
34
38
import logging
@@ -50,6 +54,33 @@ def test2(caplog):
50
54
result .stdout .no_fnmatch_line ("*log from test2*" )
51
55
52
56
57
+ def test_change_level_undos_handler_level (testdir : Testdir ) -> None :
58
+ """Ensure that 'set_level' is undone after the end of the test (handler).
59
+
60
+ Issue #7569. Tests the handler level specifically.
61
+ """
62
+ testdir .makepyfile (
63
+ """
64
+ import logging
65
+
66
+ def test1(caplog):
67
+ assert caplog.handler.level == 0
68
+ caplog.set_level(41)
69
+ assert caplog.handler.level == 41
70
+
71
+ def test2(caplog):
72
+ assert caplog.handler.level == 0
73
+
74
+ def test3(caplog):
75
+ assert caplog.handler.level == 0
76
+ caplog.set_level(43)
77
+ assert caplog.handler.level == 43
78
+ """
79
+ )
80
+ result = testdir .runpytest ()
81
+ result .assert_outcomes (passed = 3 )
82
+
83
+
53
84
def test_with_statement (caplog ):
54
85
with caplog .at_level (logging .INFO ):
55
86
logger .debug ("handler DEBUG level" )
0 commit comments