Skip to content

Commit 5dc7725

Browse files
[7.4.x] Ensure logging tests always cleanup after themselves (#11541)
Co-authored-by: Bruno Oliveira <[email protected]>
1 parent a517827 commit 5dc7725

File tree

1 file changed

+33
-23
lines changed

1 file changed

+33
-23
lines changed

Diff for: testing/logging/test_fixture.py

+33-23
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# mypy: disable-error-code="attr-defined"
2+
# mypy: disallow-untyped-defs
23
import logging
4+
from typing import Iterator
35

46
import pytest
57
from _pytest.logging import caplog_records_key
@@ -9,8 +11,8 @@
911
sublogger = logging.getLogger(__name__ + ".baz")
1012

1113

12-
@pytest.fixture
13-
def cleanup_disabled_logging():
14+
@pytest.fixture(autouse=True)
15+
def cleanup_disabled_logging() -> Iterator[None]:
1416
"""Simple fixture that ensures that a test doesn't disable logging.
1517
1618
This is necessary because ``logging.disable()`` is global, so a test disabling logging
@@ -27,7 +29,7 @@ def test_fixture_help(pytester: Pytester) -> None:
2729
result.stdout.fnmatch_lines(["*caplog*"])
2830

2931

30-
def test_change_level(caplog):
32+
def test_change_level(caplog: pytest.LogCaptureFixture) -> None:
3133
caplog.set_level(logging.INFO)
3234
logger.debug("handler DEBUG level")
3335
logger.info("handler INFO level")
@@ -42,7 +44,7 @@ def test_change_level(caplog):
4244
assert "CRITICAL" in caplog.text
4345

4446

45-
def test_change_level_logging_disabled(caplog, cleanup_disabled_logging):
47+
def test_change_level_logging_disabled(caplog: pytest.LogCaptureFixture) -> None:
4648
logging.disable(logging.CRITICAL)
4749
assert logging.root.manager.disable == logging.CRITICAL
4850
caplog.set_level(logging.WARNING)
@@ -85,9 +87,7 @@ def test2(caplog):
8587
result.stdout.no_fnmatch_line("*log from test2*")
8688

8789

88-
def test_change_disabled_level_undo(
89-
pytester: Pytester, cleanup_disabled_logging
90-
) -> None:
90+
def test_change_disabled_level_undo(pytester: Pytester) -> None:
9191
"""Ensure that '_force_enable_logging' in 'set_level' is undone after the end of the test.
9292
9393
Tests the logging output themselves (affected by disabled logging level).
@@ -144,7 +144,7 @@ def test3(caplog):
144144
result.assert_outcomes(passed=3)
145145

146146

147-
def test_with_statement(caplog):
147+
def test_with_statement(caplog: pytest.LogCaptureFixture) -> None:
148148
with caplog.at_level(logging.INFO):
149149
logger.debug("handler DEBUG level")
150150
logger.info("handler INFO level")
@@ -159,7 +159,7 @@ def test_with_statement(caplog):
159159
assert "CRITICAL" in caplog.text
160160

161161

162-
def test_with_statement_logging_disabled(caplog, cleanup_disabled_logging):
162+
def test_with_statement_logging_disabled(caplog: pytest.LogCaptureFixture) -> None:
163163
logging.disable(logging.CRITICAL)
164164
assert logging.root.manager.disable == logging.CRITICAL
165165
with caplog.at_level(logging.WARNING):
@@ -198,8 +198,8 @@ def test_with_statement_logging_disabled(caplog, cleanup_disabled_logging):
198198
],
199199
)
200200
def test_force_enable_logging_level_string(
201-
caplog, cleanup_disabled_logging, level_str, expected_disable_level
202-
):
201+
caplog: pytest.LogCaptureFixture, level_str: str, expected_disable_level: int
202+
) -> None:
203203
"""Test _force_enable_logging using a level string.
204204
205205
``expected_disable_level`` is one level below ``level_str`` because the disabled log level
@@ -218,15 +218,15 @@ def test_force_enable_logging_level_string(
218218
assert test_logger.manager.disable == expected_disable_level
219219

220220

221-
def test_log_access(caplog):
221+
def test_log_access(caplog: pytest.LogCaptureFixture) -> None:
222222
caplog.set_level(logging.INFO)
223223
logger.info("boo %s", "arg")
224224
assert caplog.records[0].levelname == "INFO"
225225
assert caplog.records[0].msg == "boo %s"
226226
assert "boo arg" in caplog.text
227227

228228

229-
def test_messages(caplog):
229+
def test_messages(caplog: pytest.LogCaptureFixture) -> None:
230230
caplog.set_level(logging.INFO)
231231
logger.info("boo %s", "arg")
232232
logger.info("bar %s\nbaz %s", "arg1", "arg2")
@@ -247,22 +247,22 @@ def test_messages(caplog):
247247
assert "Exception" not in caplog.messages[-1]
248248

249249

250-
def test_record_tuples(caplog):
250+
def test_record_tuples(caplog: pytest.LogCaptureFixture) -> None:
251251
caplog.set_level(logging.INFO)
252252
logger.info("boo %s", "arg")
253253

254254
assert caplog.record_tuples == [(__name__, logging.INFO, "boo arg")]
255255

256256

257-
def test_unicode(caplog):
257+
def test_unicode(caplog: pytest.LogCaptureFixture) -> None:
258258
caplog.set_level(logging.INFO)
259259
logger.info("bū")
260260
assert caplog.records[0].levelname == "INFO"
261261
assert caplog.records[0].msg == "bū"
262262
assert "bū" in caplog.text
263263

264264

265-
def test_clear(caplog):
265+
def test_clear(caplog: pytest.LogCaptureFixture) -> None:
266266
caplog.set_level(logging.INFO)
267267
logger.info("bū")
268268
assert len(caplog.records)
@@ -273,15 +273,19 @@ def test_clear(caplog):
273273

274274

275275
@pytest.fixture
276-
def logging_during_setup_and_teardown(caplog):
276+
def logging_during_setup_and_teardown(
277+
caplog: pytest.LogCaptureFixture,
278+
) -> Iterator[None]:
277279
caplog.set_level("INFO")
278280
logger.info("a_setup_log")
279281
yield
280282
logger.info("a_teardown_log")
281283
assert [x.message for x in caplog.get_records("teardown")] == ["a_teardown_log"]
282284

283285

284-
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:
285289
assert not caplog.records
286290
assert not caplog.get_records("call")
287291
logger.info("a_call_log")
@@ -290,25 +294,31 @@ def test_caplog_captures_for_all_stages(caplog, logging_during_setup_and_teardow
290294
assert [x.message for x in caplog.get_records("setup")] == ["a_setup_log"]
291295

292296
# This reaches into private API, don't use this type of thing in real tests!
293-
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"}
294299

295300

296-
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:
297304
logger.info("a_call_log")
298305
assert [x.message for x in caplog.get_records("call")] == ["a_call_log"]
299306
assert [x.message for x in caplog.get_records("setup")] == ["a_setup_log"]
300-
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"}
301309

302310
caplog.clear()
303311

304312
assert caplog.get_records("call") == []
305313
assert [x.message for x in caplog.get_records("setup")] == ["a_setup_log"]
306-
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"}
307316

308317
logging.info("a_call_log_after_clear")
309318
assert [x.message for x in caplog.get_records("call")] == ["a_call_log_after_clear"]
310319
assert [x.message for x in caplog.get_records("setup")] == ["a_setup_log"]
311-
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"}
312322

313323

314324
def test_ini_controls_global_log_level(pytester: Pytester) -> None:

0 commit comments

Comments
 (0)