1
1
# mypy: disable-error-code="attr-defined"
2
+ # mypy: disallow-untyped-defs
2
3
import logging
3
4
from typing import Iterator
4
5
@@ -28,7 +29,7 @@ def test_fixture_help(pytester: Pytester) -> None:
28
29
result .stdout .fnmatch_lines (["*caplog*" ])
29
30
30
31
31
- def test_change_level (caplog ) :
32
+ def test_change_level (caplog : pytest . LogCaptureFixture ) -> None :
32
33
caplog .set_level (logging .INFO )
33
34
logger .debug ("handler DEBUG level" )
34
35
logger .info ("handler INFO level" )
@@ -43,7 +44,7 @@ def test_change_level(caplog):
43
44
assert "CRITICAL" in caplog .text
44
45
45
46
46
- def test_change_level_logging_disabled (caplog ) :
47
+ def test_change_level_logging_disabled (caplog : pytest . LogCaptureFixture ) -> None :
47
48
logging .disable (logging .CRITICAL )
48
49
assert logging .root .manager .disable == logging .CRITICAL
49
50
caplog .set_level (logging .WARNING )
@@ -143,7 +144,7 @@ def test3(caplog):
143
144
result .assert_outcomes (passed = 3 )
144
145
145
146
146
- def test_with_statement (caplog ) :
147
+ def test_with_statement (caplog : pytest . LogCaptureFixture ) -> None :
147
148
with caplog .at_level (logging .INFO ):
148
149
logger .debug ("handler DEBUG level" )
149
150
logger .info ("handler INFO level" )
@@ -158,7 +159,7 @@ def test_with_statement(caplog):
158
159
assert "CRITICAL" in caplog .text
159
160
160
161
161
- def test_with_statement_logging_disabled (caplog ) :
162
+ def test_with_statement_logging_disabled (caplog : pytest . LogCaptureFixture ) -> None :
162
163
logging .disable (logging .CRITICAL )
163
164
assert logging .root .manager .disable == logging .CRITICAL
164
165
with caplog .at_level (logging .WARNING ):
@@ -196,7 +197,9 @@ def test_with_statement_logging_disabled(caplog):
196
197
("NOTVALIDLEVEL" , logging .NOTSET ),
197
198
],
198
199
)
199
- def test_force_enable_logging_level_string (caplog , level_str , expected_disable_level ):
200
+ def test_force_enable_logging_level_string (
201
+ caplog : pytest .LogCaptureFixture , level_str : str , expected_disable_level : int
202
+ ) -> None :
200
203
"""Test _force_enable_logging using a level string.
201
204
202
205
``expected_disable_level`` is one level below ``level_str`` because the disabled log level
@@ -215,15 +218,15 @@ def test_force_enable_logging_level_string(caplog, level_str, expected_disable_l
215
218
assert test_logger .manager .disable == expected_disable_level
216
219
217
220
218
- def test_log_access (caplog ) :
221
+ def test_log_access (caplog : pytest . LogCaptureFixture ) -> None :
219
222
caplog .set_level (logging .INFO )
220
223
logger .info ("boo %s" , "arg" )
221
224
assert caplog .records [0 ].levelname == "INFO"
222
225
assert caplog .records [0 ].msg == "boo %s"
223
226
assert "boo arg" in caplog .text
224
227
225
228
226
- def test_messages (caplog ) :
229
+ def test_messages (caplog : pytest . LogCaptureFixture ) -> None :
227
230
caplog .set_level (logging .INFO )
228
231
logger .info ("boo %s" , "arg" )
229
232
logger .info ("bar %s\n baz %s" , "arg1" , "arg2" )
@@ -244,22 +247,22 @@ def test_messages(caplog):
244
247
assert "Exception" not in caplog .messages [- 1 ]
245
248
246
249
247
- def test_record_tuples (caplog ) :
250
+ def test_record_tuples (caplog : pytest . LogCaptureFixture ) -> None :
248
251
caplog .set_level (logging .INFO )
249
252
logger .info ("boo %s" , "arg" )
250
253
251
254
assert caplog .record_tuples == [(__name__ , logging .INFO , "boo arg" )]
252
255
253
256
254
- def test_unicode (caplog ) :
257
+ def test_unicode (caplog : pytest . LogCaptureFixture ) -> None :
255
258
caplog .set_level (logging .INFO )
256
259
logger .info ("bū" )
257
260
assert caplog .records [0 ].levelname == "INFO"
258
261
assert caplog .records [0 ].msg == "bū"
259
262
assert "bū" in caplog .text
260
263
261
264
262
- def test_clear (caplog ) :
265
+ def test_clear (caplog : pytest . LogCaptureFixture ) -> None :
263
266
caplog .set_level (logging .INFO )
264
267
logger .info ("bū" )
265
268
assert len (caplog .records )
@@ -270,15 +273,19 @@ def test_clear(caplog):
270
273
271
274
272
275
@pytest .fixture
273
- def logging_during_setup_and_teardown (caplog ):
276
+ def logging_during_setup_and_teardown (
277
+ caplog : pytest .LogCaptureFixture ,
278
+ ) -> Iterator [None ]:
274
279
caplog .set_level ("INFO" )
275
280
logger .info ("a_setup_log" )
276
281
yield
277
282
logger .info ("a_teardown_log" )
278
283
assert [x .message for x in caplog .get_records ("teardown" )] == ["a_teardown_log" ]
279
284
280
285
281
- def test_caplog_captures_for_all_stages (caplog , logging_during_setup_and_teardown ):
286
+ def test_caplog_captures_for_all_stages (
287
+ caplog : pytest .LogCaptureFixture , logging_during_setup_and_teardown : None
288
+ ) -> None :
282
289
assert not caplog .records
283
290
assert not caplog .get_records ("call" )
284
291
logger .info ("a_call_log" )
@@ -287,25 +294,31 @@ def test_caplog_captures_for_all_stages(caplog, logging_during_setup_and_teardow
287
294
assert [x .message for x in caplog .get_records ("setup" )] == ["a_setup_log" ]
288
295
289
296
# This reaches into private API, don't use this type of thing in real tests!
290
- assert set (caplog ._item .stash [caplog_records_key ]) == {"setup" , "call" }
297
+ caplog_records = caplog ._item .stash [caplog_records_key ]
298
+ assert set (caplog_records ) == {"setup" , "call" }
291
299
292
300
293
- def test_clear_for_call_stage (caplog , logging_during_setup_and_teardown ):
301
+ def test_clear_for_call_stage (
302
+ caplog : pytest .LogCaptureFixture , logging_during_setup_and_teardown : None
303
+ ) -> None :
294
304
logger .info ("a_call_log" )
295
305
assert [x .message for x in caplog .get_records ("call" )] == ["a_call_log" ]
296
306
assert [x .message for x in caplog .get_records ("setup" )] == ["a_setup_log" ]
297
- assert set (caplog ._item .stash [caplog_records_key ]) == {"setup" , "call" }
307
+ caplog_records = caplog ._item .stash [caplog_records_key ]
308
+ assert set (caplog_records ) == {"setup" , "call" }
298
309
299
310
caplog .clear ()
300
311
301
312
assert caplog .get_records ("call" ) == []
302
313
assert [x .message for x in caplog .get_records ("setup" )] == ["a_setup_log" ]
303
- assert set (caplog ._item .stash [caplog_records_key ]) == {"setup" , "call" }
314
+ caplog_records = caplog ._item .stash [caplog_records_key ]
315
+ assert set (caplog_records ) == {"setup" , "call" }
304
316
305
317
logging .info ("a_call_log_after_clear" )
306
318
assert [x .message for x in caplog .get_records ("call" )] == ["a_call_log_after_clear" ]
307
319
assert [x .message for x in caplog .get_records ("setup" )] == ["a_setup_log" ]
308
- assert set (caplog ._item .stash [caplog_records_key ]) == {"setup" , "call" }
320
+ caplog_records = caplog ._item .stash [caplog_records_key ]
321
+ assert set (caplog_records ) == {"setup" , "call" }
309
322
310
323
311
324
def test_ini_controls_global_log_level (pytester : Pytester ) -> None :
0 commit comments