Skip to content

Commit 3dcdaab

Browse files
authored
Merge pull request #3585 from wcooley/feature/3579-caplog-messages
Add `messages` property to `caplog` fixture.
2 parents 94c41be + 3615977 commit 3dcdaab

File tree

4 files changed

+39
-0
lines changed

4 files changed

+39
-0
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ Victor Uriarte
204204
Vidar T. Fauske
205205
Vitaly Lashmanov
206206
Vlad Dragos
207+
Wil Cooley
207208
William Lee
208209
Wouter van Ackooy
209210
Xuan Luong

changelog/3579.feature.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixture ``caplog`` now has a ``messages`` property, providing convenient access to the format-interpolated log messages without the extra data provided by the formatter/handler.

src/_pytest/logging.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,22 @@ def record_tuples(self):
270270
"""
271271
return [(r.name, r.levelno, r.getMessage()) for r in self.records]
272272

273+
@property
274+
def messages(self):
275+
"""Returns a list of format-interpolated log messages.
276+
277+
Unlike 'records', which contains the format string and parameters for interpolation, log messages in this list
278+
are all interpolated.
279+
Unlike 'text', which contains the output from the handler, log messages in this list are unadorned with
280+
levels, timestamps, etc, making exact comparisions more reliable.
281+
282+
Note that traceback or stack info (from :func:`logging.exception` or the `exc_info` or `stack_info` arguments
283+
to the logging functions) is not included, as this is added by the formatter in the handler.
284+
285+
.. versionadded:: 3.7
286+
"""
287+
return [r.getMessage() for r in self.records]
288+
273289
def clear(self):
274290
"""Reset the list of log records and the captured log text."""
275291
self.handler.reset()

testing/logging/test_fixture.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,27 @@ def test_log_access(caplog):
7373
assert "boo arg" in caplog.text
7474

7575

76+
def test_messages(caplog):
77+
caplog.set_level(logging.INFO)
78+
logger.info("boo %s", "arg")
79+
logger.info("bar %s\nbaz %s", "arg1", "arg2")
80+
assert "boo arg" == caplog.messages[0]
81+
assert "bar arg1\nbaz arg2" == caplog.messages[1]
82+
assert caplog.text.count("\n") > len(caplog.messages)
83+
assert len(caplog.text.splitlines()) > len(caplog.messages)
84+
85+
try:
86+
raise Exception("test")
87+
except Exception:
88+
logger.exception("oops")
89+
90+
assert "oops" in caplog.text
91+
assert "oops" in caplog.messages[-1]
92+
# Tracebacks are stored in the record and not added until the formatter or handler.
93+
assert "Exception" in caplog.text
94+
assert "Exception" not in caplog.messages[-1]
95+
96+
7697
def test_record_tuples(caplog):
7798
caplog.set_level(logging.INFO)
7899
logger.info("boo %s", "arg")

0 commit comments

Comments
 (0)