Skip to content

Commit

Permalink
Merge pull request #3301 from ankostis/caplog_clear_text
Browse files Browse the repository at this point in the history
Fix #3297 where caplog.clear() did not clear text, just records
  • Loading branch information
RonnyPfannschmidt committed Mar 13, 2018
2 parents 3909225 + 02bec7a commit 2612d96
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 2 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ Kale Kundert
Katarzyna Jachim
Kevin Cox
Kodi B. Arfer
Kostis Anagnostopoulos
Lawrence Mitchell
Lee Kamentsky
Lev Maximov
Expand Down
13 changes: 11 additions & 2 deletions _pytest/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,10 @@ def emit(self, record):
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."""
Expand All @@ -197,6 +201,9 @@ def _finalize(self):

@property
def handler(self):
"""
:rtype: LogCaptureHandler
"""
return self._item.catch_log_handler

def get_records(self, when):
Expand Down Expand Up @@ -239,8 +246,8 @@ def record_tuples(self):
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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions changelog/3297.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fixed ``clear()`` method on ``caplog`` fixture which cleared ``records``,
but not the ``text`` property.
2 changes: 2 additions & 0 deletions testing/logging/test_fixture.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2612d96

Please sign in to comment.