Skip to content

Commit c7e784f

Browse files
author
zx.qiu
committed
Fix stage caplog records not clear
Closes #9877
1 parent aa55975 commit c7e784f

File tree

4 files changed

+27
-0
lines changed

4 files changed

+27
-0
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,5 +363,6 @@ Yuval Shimon
363363
Zac Hatfield-Dodds
364364
Zachary Kneupper
365365
Zachary OBrien
366+
Zhouxin Qiu
366367
Zoltán Máté
367368
Zsolt Cserna

changelog/9877.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed ``caplog.get_records(when)`` still get the stage records after ``caplog.clear()``

src/_pytest/logging.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,16 @@ def __init__(self) -> None:
335335
"""Create a new log handler."""
336336
super().__init__(StringIO())
337337
self.records: List[logging.LogRecord] = []
338+
self.set_when(None)
339+
340+
def set_when(self, when: Optional[str]) -> None:
341+
"""Prepare for the given test phase (setup/call/teardown)."""
342+
self._when = when
343+
344+
def get_when(self) -> Optional[str]:
345+
return self._when
346+
347+
when = property(get_when, set_when)
338348

339349
def emit(self, record: logging.LogRecord) -> None:
340350
"""Keep the log records in a list in addition to the log text."""
@@ -441,6 +451,7 @@ def messages(self) -> List[str]:
441451
def clear(self) -> None:
442452
"""Reset the list of log records and the captured log text."""
443453
self.handler.reset()
454+
self._item.stash[caplog_records_key][self.handler.when] = self.records
444455

445456
def set_level(self, level: Union[int, str], logger: Optional[str] = None) -> None:
446457
"""Set the level of a logger for the duration of a test.
@@ -695,6 +706,7 @@ def _runtest_for(self, item: nodes.Item, when: str) -> Generator[None, None, Non
695706
level=self.log_level,
696707
) as report_handler:
697708
caplog_handler.reset()
709+
caplog_handler.set_when(when)
698710
report_handler.reset()
699711
item.stash[caplog_records_key][when] = caplog_handler.records
700712
item.stash[caplog_handler_key] = caplog_handler

testing/logging/test_fixture.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,19 @@ def test_caplog_captures_for_all_stages(caplog, logging_during_setup_and_teardow
172172
assert set(caplog._item.stash[caplog_records_key]) == {"setup", "call"}
173173

174174

175+
def test_clear_for_call_stage(caplog, logging_during_setup_and_teardown):
176+
logger.info("a_call_log")
177+
assert [x.message for x in caplog.get_records("call")] == ["a_call_log"]
178+
assert [x.message for x in caplog.get_records("setup")] == ["a_setup_log"]
179+
assert set(caplog._item.stash[caplog_records_key]) == {"setup", "call"}
180+
181+
caplog.clear()
182+
183+
assert caplog.get_records("call") == []
184+
assert [x.message for x in caplog.get_records("setup")] == ["a_setup_log"]
185+
assert set(caplog._item.stash[caplog_records_key]) == {"setup", "call"}
186+
187+
175188
def test_ini_controls_global_log_level(pytester: Pytester) -> None:
176189
pytester.makepyfile(
177190
"""

0 commit comments

Comments
 (0)