forked from eisensheng/pytest-catchlog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_pytest_catchlog.py
328 lines (264 loc) · 10.3 KB
/
test_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
322
323
324
325
326
327
328
# coding: utf-8
import sys
import py
pytest_plugins = 'pytester'
def test_nothing_logged(testdir):
testdir.makepyfile('''
import sys
import logging
def test_foo():
sys.stdout.write('text going to stdout')
sys.stderr.write('text going to stderr')
assert False
''')
result = testdir.runpytest()
assert result.ret == 1
result.stdout.fnmatch_lines(['*- Captured stdout call -*',
'text going to stdout'])
result.stdout.fnmatch_lines(['*- Captured stderr call -*',
'text going to stderr'])
py.test.raises(Exception, result.stdout.fnmatch_lines,
['*- Captured *log call -*'])
def test_messages_logged(testdir):
testdir.makepyfile('''
import sys
import logging
def test_foo():
sys.stdout.write('text going to stdout')
sys.stderr.write('text going to stderr')
logging.getLogger().info('text going to logger')
assert False
''')
result = testdir.runpytest()
assert result.ret == 1
result.stdout.fnmatch_lines(['*- Captured *log call -*',
'*text going to logger*'])
result.stdout.fnmatch_lines(['*- Captured stdout call -*',
'text going to stdout'])
result.stdout.fnmatch_lines(['*- Captured stderr call -*',
'text going to stderr'])
def test_setup_logging(testdir):
testdir.makepyfile('''
import sys
import logging
def setup_function(function):
logging.getLogger().info('text going to logger from setup')
def test_foo():
logging.getLogger().info('text going to logger from call')
assert False
''')
result = testdir.runpytest()
assert result.ret == 1
result.stdout.fnmatch_lines(['*- Captured *log setup -*',
'*text going to logger from setup*',
'*- Captured *log call -*',
'*text going to logger from call*'])
def test_teardown_logging(testdir):
testdir.makepyfile('''
import sys
import logging
def test_foo():
logging.getLogger().info('text going to logger from call')
def teardown_function(function):
logging.getLogger().info('text going to logger from teardown')
assert False
''')
result = testdir.runpytest()
assert result.ret == 1
result.stdout.fnmatch_lines(['*- Captured *log call -*',
'*text going to logger from call*',
'*- Captured *log teardown -*',
'*text going to logger from teardown*'])
def test_change_level(testdir):
testdir.makepyfile('''
import sys
import logging
def test_foo(caplog):
caplog.set_level(logging.INFO)
log = logging.getLogger()
log.debug('handler DEBUG level')
log.info('handler INFO level')
caplog.set_level(logging.CRITICAL, logger='root.baz')
log = logging.getLogger('root.baz')
log.warning('logger WARNING level')
log.critical('logger CRITICAL level')
assert False
''')
result = testdir.runpytest()
assert result.ret == 1
result.stdout.fnmatch_lines(['*- Captured *log call -*',
'*handler INFO level*',
'*logger CRITICAL level*'])
py.test.raises(Exception, result.stdout.fnmatch_lines,
['*- Captured *log call -*', '*handler DEBUG level*'])
py.test.raises(Exception, result.stdout.fnmatch_lines,
['*- Captured *log call -*', '*logger WARNING level*'])
@py.test.mark.skipif('sys.version_info < (2,5)')
def test_with_statement(testdir):
testdir.makepyfile('''
from __future__ import with_statement
import sys
import logging
def test_foo(caplog):
with caplog.at_level(logging.INFO):
log = logging.getLogger()
log.debug('handler DEBUG level')
log.info('handler INFO level')
with caplog.at_level(logging.CRITICAL, logger='root.baz'):
log = logging.getLogger('root.baz')
log.warning('logger WARNING level')
log.critical('logger CRITICAL level')
assert False
''')
result = testdir.runpytest()
assert result.ret == 1
result.stdout.fnmatch_lines(['*- Captured *log call -*',
'*handler INFO level*',
'*logger CRITICAL level*'])
py.test.raises(Exception, result.stdout.fnmatch_lines,
['*- Captured *log call -*', '*handler DEBUG level*'])
py.test.raises(Exception, result.stdout.fnmatch_lines,
['*- Captured *log call -*', '*logger WARNING level*'])
def test_log_access(testdir):
testdir.makepyfile('''
import sys
import logging
def test_foo(caplog):
logging.getLogger().info('boo %s', 'arg')
assert caplog.records[0].levelname == 'INFO'
assert caplog.records[0].msg == 'boo %s'
assert 'boo arg' in caplog.text
''')
result = testdir.runpytest()
assert result.ret == 0
def test_unicode(testdir):
u = lambda x: x.decode('utf-8') if sys.version_info < (3,) else x
testdir.makepyfile(u('''
# coding: utf-8
import sys
import logging
u = lambda x: x.decode('utf-8') if sys.version_info < (3,) else x
def test_foo(caplog):
logging.getLogger().info(u('bū'))
assert caplog.records[0].levelname == 'INFO'
assert caplog.records[0].msg == u('bū')
assert u('bū') in caplog.text
'''))
result = testdir.runpytest()
assert result.ret == 0
def test_funcarg_help(testdir):
result = testdir.runpytest('--funcargs')
result.stdout.fnmatch_lines(['*caplog*'])
def test_record_tuples(testdir):
testdir.makepyfile('''
import sys
import logging
def test_foo(caplog):
logging.getLogger().info('boo %s', 'arg')
assert caplog.record_tuples == [
('root', logging.INFO, 'boo arg'),
]
''')
result = testdir.runpytest()
assert result.ret == 0
def test_compat_camel_case_aliases(testdir):
testdir.makepyfile('''
import logging
def test_foo(caplog):
caplog.setLevel(logging.INFO)
logging.getLogger().debug('boo!')
with caplog.atLevel(logging.WARNING):
logging.getLogger().info('catch me if you can')
''')
result = testdir.runpytest()
assert result.ret == 0
py.test.raises(Exception, result.stdout.fnmatch_lines,
['*- Captured *log call -*'])
result = testdir.runpytest('-rw')
assert result.ret == 0
result.stdout.fnmatch_lines('''
=*warning summary*=
*WL1*test_compat_camel_case_aliases*caplog.setLevel()*deprecated*
*WL1*test_compat_camel_case_aliases*caplog.atLevel()*deprecated*
''')
def test_compat_properties(testdir):
testdir.makepyfile('''
import logging
def test_foo(caplog):
logging.getLogger().info('boo %s', 'arg')
assert caplog.text == caplog.text() == str(caplog.text)
assert caplog.records == caplog.records() == list(caplog.records)
assert (caplog.record_tuples ==
caplog.record_tuples() == list(caplog.record_tuples))
''')
result = testdir.runpytest()
assert result.ret == 0
result = testdir.runpytest('-rw')
assert result.ret == 0
result.stdout.fnmatch_lines('''
=*warning summary*=
*WL1*test_compat_properties*caplog.text()*deprecated*
*WL1*test_compat_properties*caplog.records()*deprecated*
*WL1*test_compat_properties*caplog.record_tuples()*deprecated*
''')
def test_compat_records_modification(testdir):
testdir.makepyfile('''
import logging
logger = logging.getLogger()
def test_foo(caplog):
logger.info('boo %s', 'arg')
assert caplog.records
assert caplog.records()
del caplog.records()[:] # legacy syntax
assert not caplog.records
assert not caplog.records()
logger.info('foo %s', 'arg')
assert caplog.records
assert caplog.records()
''')
result = testdir.runpytest()
assert result.ret == 0
def test_disable_log_capturing(testdir):
testdir.makepyfile('''
import sys
import logging
def test_foo(caplog):
sys.stdout.write('text going to stdout')
logging.getLogger().warning('catch me if you can!')
sys.stderr.write('text going to stderr')
assert False
''')
result = testdir.runpytest('--no-print-logs')
print(result.stdout)
assert result.ret == 1
result.stdout.fnmatch_lines(['*- Captured stdout call -*',
'text going to stdout'])
result.stdout.fnmatch_lines(['*- Captured stderr call -*',
'text going to stderr'])
py.test.raises(Exception, result.stdout.fnmatch_lines,
['*- Captured *log call -*'])
def test_disable_log_capturing_ini(testdir):
testdir.makeini(
'''
[pytest]
log_print=False
'''
)
testdir.makepyfile('''
import sys
import logging
def test_foo(caplog):
sys.stdout.write('text going to stdout')
logging.getLogger().warning('catch me if you can!')
sys.stderr.write('text going to stderr')
assert False
''')
result = testdir.runpytest()
print(result.stdout)
assert result.ret == 1
result.stdout.fnmatch_lines(['*- Captured stdout call -*',
'text going to stdout'])
result.stdout.fnmatch_lines(['*- Captured stderr call -*',
'text going to stderr'])
py.test.raises(Exception, result.stdout.fnmatch_lines,
['*- Captured *log call -*'])