Merge pull request #3301 from ankostis/caplog_clear_text

Fix #3297 where caplog.clear() did not clear text, just records
This commit is contained in:
Ronny Pfannschmidt 2018-03-13 16:39:53 +01:00 committed by GitHub
commit 2612d967f8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 16 additions and 2 deletions

View File

@ -105,6 +105,7 @@ Kale Kundert
Katarzyna Jachim
Kevin Cox
Kodi B. Arfer
Kostis Anagnostopoulos
Lawrence Mitchell
Lee Kamentsky
Lev Maximov

View File

@ -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

View File

@ -0,0 +1,2 @@
Fixed ``clear()`` method on ``caplog`` fixture which cleared ``records``,
but not the ``text`` property.

View File

@ -95,8 +95,10 @@ def test_clear(caplog):
caplog.set_level(logging.INFO)
logger.info(u'')
assert len(caplog.records)
assert caplog.text
caplog.clear()
assert not len(caplog.records)
assert not caplog.text
@pytest.fixture