From 68375513f300e51c4ee97172a4dc4728e89d898a Mon Sep 17 00:00:00 2001 From: Kostis Anagnostopoulos Date: Tue, 13 Mar 2018 00:28:47 +0200 Subject: [PATCH 1/3] Add TC to demonstrate #3297 that caplog.clear() does not clean text --- testing/logging/test_fixture.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/testing/logging/test_fixture.py b/testing/logging/test_fixture.py index 204472c80..24576719d 100644 --- a/testing/logging/test_fixture.py +++ b/testing/logging/test_fixture.py @@ -95,8 +95,10 @@ def test_clear(caplog): caplog.set_level(logging.INFO) logger.info(u'bū') assert len(caplog.records) + assert caplog.text caplog.clear() assert not len(caplog.records) + assert not caplog.text @pytest.fixture From 5e92644f94cd3432aa1de86b905ce23748526857 Mon Sep 17 00:00:00 2001 From: Thomas Hisch Date: Mon, 12 Mar 2018 23:11:45 +0100 Subject: [PATCH 2/3] Properly reset LogCaptureHandler in caplog Closes #3297 --- _pytest/logging.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/_pytest/logging.py b/_pytest/logging.py index 6d9274d1a..c920659a5 100644 --- a/_pytest/logging.py +++ b/_pytest/logging.py @@ -176,6 +176,10 @@ class LogCaptureHandler(logging.StreamHandler): self.records.append(record) logging.StreamHandler.emit(self, record) + def reset(self): + self.records = [] + self.stream = py.io.TextIO() + class LogCaptureFixture(object): """Provides access and control of log capturing.""" @@ -197,6 +201,9 @@ class LogCaptureFixture(object): @property def handler(self): + """ + :rtype: LogCaptureHandler + """ return self._item.catch_log_handler def get_records(self, when): @@ -239,8 +246,8 @@ class LogCaptureFixture(object): return [(r.name, r.levelno, r.getMessage()) for r in self.records] def clear(self): - """Reset the list of log records.""" - self.handler.records = [] + """Reset the list of log records and the captured log text.""" + self.handler.reset() def set_level(self, level, logger=None): """Sets the level for capturing of logs. The level will be restored to its previous value at the end of @@ -285,6 +292,8 @@ def caplog(request): * caplog.text() -> string containing formatted log output * caplog.records() -> list of logging.LogRecord instances * caplog.record_tuples() -> list of (logger_name, level, message) tuples + * caplog.clear() -> clear captured records and formatted log output + string """ result = LogCaptureFixture(request.node) yield result From 02bec7a3bb96625042ec00adfe45e71d454f10f1 Mon Sep 17 00:00:00 2001 From: Kostis Anagnostopoulos Date: Tue, 13 Mar 2018 00:52:02 +0200 Subject: [PATCH 3/3] Add changelog & myself (ankostis) to authors --- AUTHORS | 1 + changelog/3297.bugfix.rst | 2 ++ 2 files changed, 3 insertions(+) create mode 100644 changelog/3297.bugfix.rst diff --git a/AUTHORS b/AUTHORS index 3a564606c..25c4384cd 100644 --- a/AUTHORS +++ b/AUTHORS @@ -105,6 +105,7 @@ Kale Kundert Katarzyna Jachim Kevin Cox Kodi B. Arfer +Kostis Anagnostopoulos Lawrence Mitchell Lee Kamentsky Lev Maximov diff --git a/changelog/3297.bugfix.rst b/changelog/3297.bugfix.rst new file mode 100644 index 000000000..f3cbc2c9c --- /dev/null +++ b/changelog/3297.bugfix.rst @@ -0,0 +1,2 @@ +Fixed ``clear()`` method on ``caplog`` fixture which cleared ``records``, +but not the ``text`` property. \ No newline at end of file