Skip to content

Commit 395bbae

Browse files
committed
Ensure logging tests always cleanup after themselves
Logging has many global states, and we did foresee this by creating a ``cleanup_disabled_logging`` fixture, however one might still forget to use it and failures leak later -- sometimes not even in the same PR, because the order of the tests might change in the future, specially when running under xdist. This problem surfaced during #11530, where tests unrelated to the change started to fail.
1 parent ee53433 commit 395bbae

File tree

1 file changed

+7
-10
lines changed

1 file changed

+7
-10
lines changed

testing/logging/test_fixture.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# mypy: disable-error-code="attr-defined"
22
import logging
3+
from typing import Iterator
34

45
import pytest
56
from _pytest.logging import caplog_records_key
@@ -9,8 +10,8 @@
910
sublogger = logging.getLogger(__name__ + ".baz")
1011

1112

12-
@pytest.fixture
13-
def cleanup_disabled_logging():
13+
@pytest.fixture(autouse=True)
14+
def cleanup_disabled_logging() -> Iterator[None]:
1415
"""Simple fixture that ensures that a test doesn't disable logging.
1516
1617
This is necessary because ``logging.disable()`` is global, so a test disabling logging
@@ -42,7 +43,7 @@ def test_change_level(caplog):
4243
assert "CRITICAL" in caplog.text
4344

4445

45-
def test_change_level_logging_disabled(caplog, cleanup_disabled_logging):
46+
def test_change_level_logging_disabled(caplog):
4647
logging.disable(logging.CRITICAL)
4748
assert logging.root.manager.disable == logging.CRITICAL
4849
caplog.set_level(logging.WARNING)
@@ -85,9 +86,7 @@ def test2(caplog):
8586
result.stdout.no_fnmatch_line("*log from test2*")
8687

8788

88-
def test_change_disabled_level_undo(
89-
pytester: Pytester, cleanup_disabled_logging
90-
) -> None:
89+
def test_change_disabled_level_undo(pytester: Pytester) -> None:
9190
"""Ensure that '_force_enable_logging' in 'set_level' is undone after the end of the test.
9291
9392
Tests the logging output themselves (affected by disabled logging level).
@@ -159,7 +158,7 @@ def test_with_statement(caplog):
159158
assert "CRITICAL" in caplog.text
160159

161160

162-
def test_with_statement_logging_disabled(caplog, cleanup_disabled_logging):
161+
def test_with_statement_logging_disabled(caplog):
163162
logging.disable(logging.CRITICAL)
164163
assert logging.root.manager.disable == logging.CRITICAL
165164
with caplog.at_level(logging.WARNING):
@@ -197,9 +196,7 @@ def test_with_statement_logging_disabled(caplog, cleanup_disabled_logging):
197196
("NOTVALIDLEVEL", logging.NOTSET),
198197
],
199198
)
200-
def test_force_enable_logging_level_string(
201-
caplog, cleanup_disabled_logging, level_str, expected_disable_level
202-
):
199+
def test_force_enable_logging_level_string(caplog, level_str, expected_disable_level):
203200
"""Test _force_enable_logging using a level string.
204201
205202
``expected_disable_level`` is one level below ``level_str`` because the disabled log level

0 commit comments

Comments
 (0)