diff --git a/AUTHORS b/AUTHORS index d1f1b5503f2..7d27f5adadf 100644 --- a/AUTHORS +++ b/AUTHORS @@ -363,5 +363,6 @@ Yuval Shimon Zac Hatfield-Dodds Zachary Kneupper Zachary OBrien +Zhouxin Qiu Zoltán Máté Zsolt Cserna diff --git a/changelog/9877.bugfix.rst b/changelog/9877.bugfix.rst new file mode 100644 index 00000000000..ace3a4385a4 --- /dev/null +++ b/changelog/9877.bugfix.rst @@ -0,0 +1 @@ +Fixed ``caplog.get_records(when)`` still get the stage records after ``caplog.clear()`` diff --git a/doc/en/conf.py b/doc/en/conf.py index 0022363b27e..3c26b8ce0b7 100644 --- a/doc/en/conf.py +++ b/doc/en/conf.py @@ -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 --------------------------------------------------- diff --git a/src/_pytest/logging.py b/src/_pytest/logging.py index a4f4214b137..96709ae93cf 100644 --- a/src/_pytest/logging.py +++ b/src/_pytest/logging.py @@ -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.""" @@ -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. @@ -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 diff --git a/testing/logging/test_fixture.py b/testing/logging/test_fixture.py index bcb20de5805..6b85ce60a63 100644 --- a/testing/logging/test_fixture.py +++ b/testing/logging/test_fixture.py @@ -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( """