Skip to content

Commit

Permalink
Fix stage caplog records not clear
Browse files Browse the repository at this point in the history
  • Loading branch information
zx.qiu committed Jun 15, 2022
1 parent fab696d commit b0b313f
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 1 deletion.
1 change: 1 addition & 0 deletions AUTHORS
Expand Up @@ -363,5 +363,6 @@ Yuval Shimon
Zac Hatfield-Dodds
Zachary Kneupper
Zachary OBrien
Zhouxin Qiu
Zoltán Máté
Zsolt Cserna
1 change: 1 addition & 0 deletions changelog/9877.bugfix.rst
@@ -0,0 +1 @@
Fixed ``caplog.get_records(when)`` still get the stage records after ``caplog.clear()``
4 changes: 3 additions & 1 deletion doc/en/conf.py
Expand Up @@ -320,7 +320,9 @@

# One entry per manual page. List of tuples
# (source start file, name, description, authors, manual section).
man_pages = [("how-to/usage", "pytest", "pytest usage", ["holger krekel at merlinux eu"], 1)]
man_pages = [
("how-to/usage", "pytest", "pytest usage", ["holger krekel at merlinux eu"], 1)
]


# -- Options for Epub output ---------------------------------------------------
Expand Down
12 changes: 12 additions & 0 deletions src/_pytest/logging.py
Expand Up @@ -335,6 +335,16 @@ def __init__(self) -> None:
"""Create a new log handler."""
super().__init__(StringIO())
self.records: List[logging.LogRecord] = []
self.set_when(None)

def set_when(self, when: Optional[str]) -> None:
"""Prepare for the given test phase (setup/call/teardown)."""
self._when = when

def get_when(self) -> Optional[str]:
return self._when

when = property(get_when, set_when)

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

def set_level(self, level: Union[int, str], logger: Optional[str] = None) -> None:
"""Set the level of a logger for the duration of a test.
Expand Down Expand Up @@ -695,6 +706,7 @@ def _runtest_for(self, item: nodes.Item, when: str) -> Generator[None, None, Non
level=self.log_level,
) as report_handler:
caplog_handler.reset()
caplog_handler.set_when(when)
report_handler.reset()
item.stash[caplog_records_key][when] = caplog_handler.records
item.stash[caplog_handler_key] = caplog_handler
Expand Down
13 changes: 13 additions & 0 deletions testing/logging/test_fixture.py
Expand Up @@ -172,6 +172,19 @@ def test_caplog_captures_for_all_stages(caplog, logging_during_setup_and_teardow
assert set(caplog._item.stash[caplog_records_key]) == {"setup", "call"}


def test_clear_for_call_stage(caplog, logging_during_setup_and_teardown):
logger.info("a_call_log")
assert [x.message for x in caplog.get_records("call")] == ["a_call_log"]
assert [x.message for x in caplog.get_records("setup")] == ["a_setup_log"]
assert set(caplog._item.stash[caplog_records_key]) == {"setup", "call"}

caplog.clear()

assert caplog.get_records("call") == []
assert [x.message for x in caplog.get_records("setup")] == ["a_setup_log"]
assert set(caplog._item.stash[caplog_records_key]) == {"setup", "call"}


def test_ini_controls_global_log_level(pytester: Pytester) -> None:
pytester.makepyfile(
"""
Expand Down

0 comments on commit b0b313f

Please sign in to comment.