Skip to content

Commit 15cbd61

Browse files
committed
Change caplog.get_handler(when) to caplog.get_records(when)
While updating the docs I noticed that caplog.get_handler() exposes the underlying Handler object, which I think it is a bit too much detail at this stage. Update to return the records directly instead.
1 parent 2f955e0 commit 15cbd61

File tree

4 files changed

+35
-10
lines changed

4 files changed

+35
-10
lines changed

_pytest/logging.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,12 +144,23 @@ def _finalize(self):
144144
def handler(self):
145145
return self._item.catch_log_handler
146146

147-
def get_handler(self, when):
147+
def get_records(self, when):
148148
"""
149-
Get the handler for a specified state of the tests.
150-
Valid values for the when parameter are: 'setup', 'call' and 'teardown'.
149+
Get the logging records for one of the possible test phases.
150+
151+
:param str when:
152+
Which test phase to obtain the records from. Valid values are: "setup", "call" and "teardown".
153+
154+
:rtype: List[logging.LogRecord]
155+
:return: the list of captured records at the given stage
156+
157+
.. versionadded:: 3.4
151158
"""
152-
return self._item.catch_log_handlers.get(when)
159+
handler = self._item.catch_log_handlers.get(when)
160+
if handler:
161+
return handler.records
162+
else:
163+
return []
153164

154165
@property
155166
def text(self):

changelog/3117.feature

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
New ``caplog.get_handler(when)`` method which provides access to the underlying ``Handler`` class used to capture logging during each testing stage, allowing users to obtain the captured records during ``"setup"`` and ``"teardown"`` stages.
1+
New ``caplog.get_records(when)`` method which provides access the captured records during each testing stage: ``"setup"``, ``"call"`` and ``"teardown"`` stages.

doc/en/logging.rst

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,21 @@ The ``caplop.records`` attribute contains records from the current stage only, s
143143
inside the ``setup`` phase it contains only setup logs, same with the ``call`` and
144144
``teardown`` phases.
145145

146-
It is possible to access logs from other stages with ``caplog.get_handler('setup').records``.
146+
To access logs from other stages, use the ``caplog.get_records(when)`` method. As an example,
147+
if you want to make sure that tests which use a certain fixture never log any warnings, you can inspect
148+
the records for the ``setup`` and ``call`` stages during teardown like so:
149+
150+
.. code-block:: python
151+
152+
153+
@pytest.fixture
154+
def window(caplog):
155+
window = create_window()
156+
yield window
157+
for when in ('setup', 'call'):
158+
messages = [x.message for x in caplog.get_records(when) if x.level == logging.WARNING]
159+
if messages:
160+
pytest.fail('warning messages encountered during testing: {}'.format(messages))
147161
148162
149163
caplog fixture API

testing/logging/test_fixture.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,16 +105,16 @@ def logging_during_setup_and_teardown(caplog):
105105
logger.info('a_setup_log')
106106
yield
107107
logger.info('a_teardown_log')
108-
assert [x.message for x in caplog.get_handler('teardown').records] == ['a_teardown_log']
108+
assert [x.message for x in caplog.get_records('teardown')] == ['a_teardown_log']
109109

110110

111111
def test_caplog_captures_for_all_stages(caplog, logging_during_setup_and_teardown):
112112
assert not caplog.records
113-
assert not caplog.get_handler('call').records
113+
assert not caplog.get_records('call')
114114
logger.info('a_call_log')
115-
assert [x.message for x in caplog.get_handler('call').records] == ['a_call_log']
115+
assert [x.message for x in caplog.get_records('call')] == ['a_call_log']
116116

117-
assert [x.message for x in caplog.get_handler('setup').records] == ['a_setup_log']
117+
assert [x.message for x in caplog.get_records('setup')] == ['a_setup_log']
118118

119119
# This reachers into private API, don't use this type of thing in real tests!
120120
assert set(caplog._item.catch_log_handlers.keys()) == {'setup', 'call'}

0 commit comments

Comments
 (0)