@@ -49,6 +49,66 @@ def test_foo():
49
49
'text going to stderr' ])
50
50
51
51
52
+ def test_root_logger_affected (testdir ):
53
+ testdir .makepyfile ("""
54
+ import logging
55
+ logger = logging.getLogger()
56
+ def test_foo():
57
+ logger.info('info text ' + 'going to logger')
58
+ logger.warning('warning text ' + 'going to logger')
59
+ logger.error('error text ' + 'going to logger')
60
+
61
+ assert 0
62
+ """ )
63
+ log_file = testdir .tmpdir .join ('pytest.log' ).strpath
64
+ result = testdir .runpytest ('--log-level=ERROR' , '--log-file=pytest.log' )
65
+ assert result .ret == 1
66
+
67
+ # the capture log calls in the stdout section only contain the
68
+ # logger.error msg, because --log-level=ERROR
69
+ result .stdout .fnmatch_lines (['*error text going to logger*' ])
70
+ with pytest .raises (pytest .fail .Exception ):
71
+ result .stdout .fnmatch_lines (['*warning text going to logger*' ])
72
+ with pytest .raises (pytest .fail .Exception ):
73
+ result .stdout .fnmatch_lines (['*info text going to logger*' ])
74
+
75
+ # the log file should contain the warning and the error log messages and
76
+ # not the info one, because the default level of the root logger is
77
+ # WARNING.
78
+ assert os .path .isfile (log_file )
79
+ with open (log_file ) as rfh :
80
+ contents = rfh .read ()
81
+ assert "info text going to logger" not in contents
82
+ assert "warning text going to logger" in contents
83
+ assert "error text going to logger" in contents
84
+
85
+
86
+ def test_log_cli_level_log_level_interaction (testdir ):
87
+ testdir .makepyfile ("""
88
+ import logging
89
+ logger = logging.getLogger()
90
+
91
+ def test_foo():
92
+ logger.debug('debug text ' + 'going to logger')
93
+ logger.info('info text ' + 'going to logger')
94
+ logger.warning('warning text ' + 'going to logger')
95
+ logger.error('error text ' + 'going to logger')
96
+ assert 0
97
+ """ )
98
+
99
+ result = testdir .runpytest ('--log-cli-level=INFO' , '--log-level=ERROR' )
100
+ assert result .ret == 1
101
+
102
+ result .stdout .fnmatch_lines ([
103
+ '*-- live log call --*' ,
104
+ '*INFO*info text going to logger' ,
105
+ '*WARNING*warning text going to logger' ,
106
+ '*ERROR*error text going to logger' ,
107
+ '=* 1 failed in *=' ,
108
+ ])
109
+ assert 'DEBUG' not in result .stdout .str ()
110
+
111
+
52
112
def test_setup_logging (testdir ):
53
113
testdir .makepyfile ('''
54
114
import logging
@@ -61,7 +121,7 @@ def setup_function(function):
61
121
def test_foo():
62
122
logger.info('text going to logger from call')
63
123
assert False
64
- ''' )
124
+ ''' )
65
125
result = testdir .runpytest ('--log-level=INFO' )
66
126
assert result .ret == 1
67
127
result .stdout .fnmatch_lines (['*- Captured *log setup -*' ,
0 commit comments