From c7e784f95d6ff7dc1e9d1a1dbc234a39c0125816 Mon Sep 17 00:00:00 2001 From: "zx.qiu" Date: Wed, 15 Jun 2022 23:11:55 +0800 Subject: [PATCH] Fix stage caplog records not clear Closes #9877 --- AUTHORS | 1 + changelog/9877.bugfix.rst | 1 + src/_pytest/logging.py | 12 ++++++++++++ testing/logging/test_fixture.py | 13 +++++++++++++ 4 files changed, 27 insertions(+) create mode 100644 changelog/9877.bugfix.rst diff --git a/AUTHORS b/AUTHORS index d1f1b5503..7d27f5ada 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 000000000..ace3a4385 --- /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/src/_pytest/logging.py b/src/_pytest/logging.py index a4f4214b1..96709ae93 100644 --- a/src/_pytest/logging.py +++ b/src/_pytest/logging.py @@ -335,6 +335,16 @@ class LogCaptureHandler(logging_StreamHandler): """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 @@ class LogCaptureFixture: 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 @@ class LoggingPlugin: 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 bcb20de58..6b85ce60a 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( """