forked from eisensheng/pytest-catchlog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pytest_catchlog.py
321 lines (241 loc) · 9.73 KB
/
pytest_catchlog.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function
import functools
import logging
from contextlib import closing, contextmanager
import pytest
import py
__version__ = '1.2.2'
def get_logger_obj(logger=None):
"""Get a logger object that can be specified by its name, or passed as is.
Defaults to the root logger.
"""
if logger is None or isinstance(logger, py.builtin._basestring):
logger = logging.getLogger(logger)
return logger
@contextmanager
def logging_at_level(level, logger=None):
"""Context manager that sets the level for capturing of logs."""
logger = get_logger_obj(logger)
orig_level = logger.level
logger.setLevel(level)
try:
yield
finally:
logger.setLevel(orig_level)
@contextmanager
def logging_using_handler(handler, logger=None):
"""Context manager that safely register a given handler."""
logger = get_logger_obj(logger)
if handler in logger.handlers: # reentrancy
# Adding the same handler twice would confuse logging system.
# Just don't do that.
yield
else:
logger.addHandler(handler)
try:
yield
finally:
logger.removeHandler(handler)
@contextmanager
def catching_logs(handler, filter=None, formatter=None,
level=logging.NOTSET, logger=None):
"""Context manager that prepares the whole logging machinery properly."""
logger = get_logger_obj(logger)
if filter is not None:
handler.addFilter(filter)
if formatter is not None:
handler.setFormatter(formatter)
handler.setLevel(level)
with closing(handler):
with logging_using_handler(handler, logger):
with logging_at_level(min(handler.level, logger.level), logger):
yield handler
def add_option_ini(parser, option, dest, default=None, **kwargs):
parser.addini(dest, default=default,
help='default value for ' + option)
parser.getgroup('catchlog').addoption(option, dest=dest, **kwargs)
def get_option_ini(config, name):
ret = config.getoption(name) # 'default' arg won't work as expected
if ret is None:
ret = config.getini(name)
return ret
def pytest_addoption(parser):
"""Add options to control log capturing."""
group = parser.getgroup('catchlog', 'Log catching')
add_option_ini(parser,
'--no-print-logs', default=True,
dest='log_print', action='store_const', const=False,
help='disable printing caught logs on failed tests.')
add_option_ini(parser,
'--log-format',
dest='log_format', default=('%(filename)-25s %(lineno)4d'
' %(levelname)-8s %(message)s'),
help='log format as used by the logging module.')
add_option_ini(parser,
'--log-date-format',
dest='log_date_format', default=None,
help='log date format as used by the logging module.')
def pytest_configure(config):
"""Always register the log catcher plugin with py.test or tests can't
find the fixture function.
"""
config.pluginmanager.register(CatchLogPlugin(config), '_catch_log')
class CatchLogPlugin(object):
"""Attaches to the logging module and captures log messages for each test.
"""
def __init__(self, config):
"""Creates a new plugin to capture log messages.
The formatter can be safely shared across all handlers so
create a single one for the entire test session here.
"""
print_logs = get_option_ini(config, 'log_print')
if not isinstance(print_logs, bool):
if print_logs.lower() in ('true', 'yes', '1'):
print_logs = True
elif print_logs.lower() in ('false', 'no', '0'):
print_logs = False
self.print_logs = print_logs
self.formatter = logging.Formatter(
get_option_ini(config, 'log_format'),
get_option_ini(config, 'log_date_format'))
@contextmanager
def _runtest_for(self, item, when):
"""Implements the internals of pytest_runtest_xxx() hook."""
with catching_logs(LogCaptureHandler(),
formatter=self.formatter) as log_handler:
item.catch_log_handler = log_handler
try:
yield # run test
finally:
del item.catch_log_handler
if self.print_logs:
# Add a captured log section to the report.
log = log_handler.stream.getvalue().strip()
item.add_report_section(when, 'log', log)
@pytest.mark.hookwrapper
def pytest_runtest_setup(self, item):
with self._runtest_for(item, 'setup'):
yield
@pytest.mark.hookwrapper
def pytest_runtest_call(self, item):
with self._runtest_for(item, 'call'):
yield
@pytest.mark.hookwrapper
def pytest_runtest_teardown(self, item):
with self._runtest_for(item, 'teardown'):
yield
class LogCaptureHandler(logging.StreamHandler):
"""A logging handler that stores log records and the log text."""
def __init__(self):
"""Creates a new log handler."""
logging.StreamHandler.__init__(self)
self.stream = py.io.TextIO()
self.records = []
def close(self):
"""Close this log handler and its underlying stream."""
logging.StreamHandler.close(self)
self.stream.close()
def emit(self, record):
"""Keep the log records in a list in addition to the log text."""
self.records.append(record)
logging.StreamHandler.emit(self, record)
class LogCaptureFixture(object):
"""Provides access and control of log capturing."""
@property
def handler(self):
return self._item.catch_log_handler
def __init__(self, item):
"""Creates a new funcarg."""
self._item = item
@property
def text(self):
"""Returns the log text."""
return self.handler.stream.getvalue()
@property
def records(self):
"""Returns the list of log records."""
return self.handler.records
@property
def record_tuples(self):
"""Returns a list of a striped down version of log records intended
for use in assertion comparison.
The format of the tuple is:
(logger_name, log_level, message)
"""
return [(r.name, r.levelno, r.getMessage()) for r in self.records]
def set_level(self, level, logger=None):
"""Sets the level for capturing of logs.
By default, the level is set on the handler used to capture
logs. Specify a logger name to instead set the level of any
logger.
"""
obj = logger and logging.getLogger(logger) or self.handler
obj.setLevel(level)
def at_level(self, level, logger=None):
"""Context manager that sets the level for capturing of logs.
By default, the level is set on the handler used to capture
logs. Specify a logger name to instead set the level of any
logger.
"""
obj = logger and logging.getLogger(logger) or self.handler
return logging_at_level(level, obj)
class CallablePropertyMixin(object):
"""Backward compatibility for functions that became properties."""
@classmethod
def compat_property(cls, func):
if isinstance(func, property):
make_property = func.getter
func = func.fget
else:
make_property = property
@functools.wraps(func)
def getter(self):
naked_value = func(self)
ret = cls(naked_value)
ret._naked_value = naked_value
ret._warn_compat = self._warn_compat
ret._prop_name = func.__name__
return ret
return make_property(getter)
def __call__(self):
self._warn_compat(old="'caplog.{0}()' syntax".format(self._prop_name),
new="'caplog.{0}' property".format(self._prop_name))
return self._naked_value # to let legacy clients modify the object
class CallableList(CallablePropertyMixin, list):
pass
class CallableStr(CallablePropertyMixin, py.builtin.text):
pass
class CompatLogCaptureFixture(LogCaptureFixture):
"""Backward compatibility with pytest-capturelog."""
def _warn_compat(self, old, new):
self._item.warn(code='L1',
message=("{0} is deprecated, use {1} instead"
.format(old, new)))
@CallableStr.compat_property
def text(self):
return super(CompatLogCaptureFixture, self).text
@CallableList.compat_property
def records(self):
return super(CompatLogCaptureFixture, self).records
@CallableList.compat_property
def record_tuples(self):
return super(CompatLogCaptureFixture, self).record_tuples
def setLevel(self, level, logger=None):
self._warn_compat(old="'caplog.setLevel()'",
new="'caplog.set_level()'")
return self.set_level(level, logger)
def atLevel(self, level, logger=None):
self._warn_compat(old="'caplog.atLevel()'",
new="'caplog.at_level()'")
return self.at_level(level, logger)
@pytest.fixture
def caplog(request):
"""Access and control log capturing.
Captured logs are available through the following methods::
* caplog.text() -> string containing formatted log output
* caplog.records() -> list of logging.LogRecord instances
* caplog.record_tuples() -> list of (logger_name, level, message) tuples
"""
return CompatLogCaptureFixture(request.node)
capturelog = caplog