Skip to content

Commit d07f50b

Browse files
committed
Create a fixture used by all tests that ensure logging state is correct
1 parent 2fe84f5 commit d07f50b

2 files changed

Lines changed: 30 additions & 6 deletions

File tree

tests/unit/conftest.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
22

33
from __future__ import annotations
44

5+
import logging
56
from collections.abc import Generator
67

78
import pytest
89
from pytest_mock import AsyncMockType, MockerFixture
910

1011
from configuration import AppConfig
12+
from constants import DEFAULT_LOGGER_NAME
13+
from log import setup_logging
1114

1215
type AgentFixtures = Generator[
1316
tuple[
@@ -19,6 +22,33 @@
1922
]
2023

2124

25+
@pytest.fixture(autouse=True)
26+
def reset_logging_state():
27+
"""Reset logging state before and after each test.
28+
29+
Module-level calls to setup_logging() (such as from importing lightspeed_stack)
30+
set propagate=False on the application logger, which prevents caplog from
31+
capturing log records.
32+
33+
This fixture ensures propagation is enabled during tests and restores the
34+
original logger state afterward. It also clears the setup_logging lru_cache
35+
so tests that call setup_logging() get a fresh configuration.
36+
"""
37+
setup_logging.cache_clear()
38+
logger = logging.getLogger(DEFAULT_LOGGER_NAME)
39+
original_propagate = logger.propagate
40+
original_handlers = logger.handlers[:]
41+
original_level = logger.level
42+
logger.propagate = True
43+
44+
yield
45+
46+
setup_logging.cache_clear()
47+
logger.propagate = original_propagate
48+
logger.handlers = original_handlers
49+
logger.level = original_level
50+
51+
2252
@pytest.fixture(name="prepare_agent_mocks", scope="function")
2353
def prepare_agent_mocks_fixture(
2454
mocker: MockerFixture,

tests/unit/test_log.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,6 @@
1111
from log import get_logger, resolve_log_level, setup_logging
1212

1313

14-
@pytest.fixture(autouse=True)
15-
def clear_logging_cache():
16-
"""Clear logging cache"""
17-
setup_logging.cache_clear()
18-
19-
2014
def test_get_logger() -> None:
2115
"""Check the function to retrieve logger."""
2216
setup_logging()

0 commit comments

Comments
 (0)