Skip to content

Commit 3058318

Browse files
author
Andrei Neagu
committed
refacto test to work regardless of logging configuration
1 parent a8bcf5a commit 3058318

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

packages/service-library/tests/test_logging_utils.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# pylint:disable=redefined-outer-name
22

33
import logging
4+
from collections.abc import Iterable
45
from contextlib import suppress
56
from pathlib import Path
67
from typing import Any
@@ -244,14 +245,33 @@ def test_log_context(
244245
assert len(caplog.messages) == 2
245246

246247

248+
@pytest.fixture
249+
def log_format_with_module_name() -> Iterable[None]:
250+
for handler in logging.root.handlers:
251+
original_formatter = handler.formatter
252+
handler.setFormatter(
253+
logging.Formatter(
254+
"%(asctime)s %(levelname)s %(module)s:%(filename)s:%(lineno)d %(message)s",
255+
datefmt="%Y-%m-%d %H:%M:%S",
256+
)
257+
)
258+
259+
yield
260+
261+
for handler in logging.root.handlers:
262+
handler.formatter = original_formatter
263+
264+
247265
def test_log_context_caller_is_included_in_log(
248266
caplog: pytest.LogCaptureFixture,
267+
log_format_with_module_name: None,
249268
):
250269
caplog.clear()
251270

252271
with log_context(_logger, logging.ERROR, "a test message"):
253272
...
254273

274+
# Verify file name is in the log
255275
assert Path(__file__).name in caplog.text
256276

257277

0 commit comments

Comments
 (0)