Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #3297 where caplog.clear() did not clear text, just records #3301

Merged
merged 3 commits into from
Mar 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For consistency perhaps we should call this clear too?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But i see that there pre-existed also _LiveLoggingStreamHandler.reset() method,
so maybe @Thisch had a reason to use this name.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I didn't notice that, never mind then.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that the docstring of LogCaptureFixure.clear starts with "reset" ;) There is no specific reson why I chose "reset" in my initial commit, though.

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