5
5
from __future__ import absolute_import , division , print_function
6
6
7
7
import itertools
8
- from _pytest .main import EXIT_OK , EXIT_TESTSFAILED , EXIT_INTERRUPTED , \
9
- EXIT_USAGEERROR , EXIT_NOTESTSCOLLECTED
10
- import pytest
11
- import py
12
- import six
8
+ import platform
13
9
import sys
14
10
import time
15
- import platform
11
+ import warnings
12
+
13
+ import py
14
+ import six
15
+
16
16
import pluggy
17
+ import pytest
18
+ from _pytest .main import EXIT_OK , EXIT_TESTSFAILED , EXIT_INTERRUPTED , \
19
+ EXIT_USAGEERROR , EXIT_NOTESTSCOLLECTED
17
20
18
21
19
22
def pytest_addoption (parser ):
@@ -136,13 +139,22 @@ def __init__(self, config, file=None):
136
139
self .startdir = py .path .local ()
137
140
if file is None :
138
141
file = sys .stdout
139
- self ._tw = self .writer = _pytest .config .create_terminal_writer (config ,
140
- file )
142
+ self ._writer = _pytest .config .create_terminal_writer (config , file )
141
143
self .currentfspath = None
142
144
self .reportchars = getreportopt (config )
143
- self .hasmarkup = self ._tw .hasmarkup
145
+ self .hasmarkup = self .writer .hasmarkup
144
146
self .isatty = file .isatty ()
145
147
148
+ @property
149
+ def writer (self ):
150
+ return self ._writer
151
+
152
+ @property
153
+ def _tw (self ):
154
+ warnings .warn (DeprecationWarning ('TerminalReporter._tw is deprecated, use TerminalReporter.writer instead' ),
155
+ stacklevel = 2 )
156
+ return self .writer
157
+
146
158
def hasopt (self , char ):
147
159
char = {'xfailed' : 'x' , 'skipped' : 's' }.get (char , char )
148
160
return char in self .reportchars
@@ -152,32 +164,32 @@ def write_fspath_result(self, nodeid, res):
152
164
if fspath != self .currentfspath :
153
165
self .currentfspath = fspath
154
166
fspath = self .startdir .bestrelpath (fspath )
155
- self ._tw .line ()
156
- self ._tw .write (fspath + " " )
157
- self ._tw .write (res )
167
+ self .writer .line ()
168
+ self .writer .write (fspath + " " )
169
+ self .writer .write (res )
158
170
159
171
def write_ensure_prefix (self , prefix , extra = "" , ** kwargs ):
160
172
if self .currentfspath != prefix :
161
- self ._tw .line ()
173
+ self .writer .line ()
162
174
self .currentfspath = prefix
163
- self ._tw .write (prefix )
175
+ self .writer .write (prefix )
164
176
if extra :
165
- self ._tw .write (extra , ** kwargs )
177
+ self .writer .write (extra , ** kwargs )
166
178
self .currentfspath = - 2
167
179
168
180
def ensure_newline (self ):
169
181
if self .currentfspath :
170
- self ._tw .line ()
182
+ self .writer .line ()
171
183
self .currentfspath = None
172
184
173
185
def write (self , content , ** markup ):
174
- self ._tw .write (content , ** markup )
186
+ self .writer .write (content , ** markup )
175
187
176
188
def write_line (self , line , ** markup ):
177
189
if not isinstance (line , six .text_type ):
178
190
line = six .text_type (line , errors = "replace" )
179
191
self .ensure_newline ()
180
- self ._tw .line (line , ** markup )
192
+ self .writer .line (line , ** markup )
181
193
182
194
def rewrite (self , line , ** markup ):
183
195
"""
@@ -190,22 +202,22 @@ def rewrite(self, line, **markup):
190
202
"""
191
203
erase = markup .pop ('erase' , False )
192
204
if erase :
193
- fill_count = self ._tw .fullwidth - len (line )
205
+ fill_count = self .writer .fullwidth - len (line )
194
206
fill = ' ' * fill_count
195
207
else :
196
208
fill = ''
197
209
line = str (line )
198
- self ._tw .write ("\r " + line + fill , ** markup )
210
+ self .writer .write ("\r " + line + fill , ** markup )
199
211
200
212
def write_sep (self , sep , title = None , ** markup ):
201
213
self .ensure_newline ()
202
- self ._tw .sep (sep , title , ** markup )
214
+ self .writer .sep (sep , title , ** markup )
203
215
204
216
def section (self , title , sep = "=" , ** kw ):
205
- self ._tw .sep (sep , title , ** kw )
217
+ self .writer .sep (sep , title , ** kw )
206
218
207
219
def line (self , msg , ** kw ):
208
- self ._tw .line (msg , ** kw )
220
+ self .writer .line (msg , ** kw )
209
221
210
222
def pytest_internalerror (self , excrepr ):
211
223
for line in six .text_type (excrepr ).split ("\n " ):
@@ -252,7 +264,7 @@ def pytest_runtest_logreport(self, report):
252
264
if not hasattr (rep , 'node' ) and self .showfspath :
253
265
self .write_fspath_result (rep .nodeid , letter )
254
266
else :
255
- self ._tw .write (letter )
267
+ self .writer .write (letter )
256
268
else :
257
269
if isinstance (word , tuple ):
258
270
word , markup = word
@@ -263,16 +275,18 @@ def pytest_runtest_logreport(self, report):
263
275
markup = {'red' : True }
264
276
elif rep .skipped :
265
277
markup = {'yellow' : True }
278
+ else :
279
+ markup = {}
266
280
line = self ._locationline (rep .nodeid , * rep .location )
267
281
if not hasattr (rep , 'node' ):
268
282
self .write_ensure_prefix (line , word , ** markup )
269
- # self._tw .write(word, **markup)
283
+ # self.writer .write(word, **markup)
270
284
else :
271
285
self .ensure_newline ()
272
286
if hasattr (rep , 'node' ):
273
- self ._tw .write ("[%s] " % rep .node .gateway .id )
274
- self ._tw .write (word , ** markup )
275
- self ._tw .write (" " + line )
287
+ self .writer .write ("[%s] " % rep .node .gateway .id )
288
+ self .writer .write (word , ** markup )
289
+ self .writer .write (" " + line )
276
290
self .currentfspath = - 2
277
291
278
292
def pytest_collection (self ):
@@ -358,9 +372,9 @@ def pytest_collection_finish(self, session):
358
372
if self .config .option .collectonly :
359
373
self ._printcollecteditems (session .items )
360
374
if self .stats .get ('failed' ):
361
- self ._tw .sep ("!" , "collection failures" )
375
+ self .writer .sep ("!" , "collection failures" )
362
376
for rep in self .stats .get ('failed' ):
363
- rep .toterminal (self ._tw )
377
+ rep .toterminal (self .writer )
364
378
return 1
365
379
return 0
366
380
lines = self .config .hook .pytest_report_collectionfinish (
@@ -378,12 +392,12 @@ def _printcollecteditems(self, items):
378
392
name = item .nodeid .split ('::' , 1 )[0 ]
379
393
counts [name ] = counts .get (name , 0 ) + 1
380
394
for name , count in sorted (counts .items ()):
381
- self ._tw .line ("%s: %d" % (name , count ))
395
+ self .writer .line ("%s: %d" % (name , count ))
382
396
else :
383
397
for item in items :
384
398
nodeid = item .nodeid
385
399
nodeid = nodeid .replace ("::()::" , "::" )
386
- self ._tw .line (nodeid )
400
+ self .writer .line (nodeid )
387
401
return
388
402
stack = []
389
403
indent = ""
@@ -398,13 +412,13 @@ def _printcollecteditems(self, items):
398
412
# if col.name == "()":
399
413
# continue
400
414
indent = (len (stack ) - 1 ) * " "
401
- self ._tw .line ("%s%s" % (indent , col ))
415
+ self .writer .line ("%s%s" % (indent , col ))
402
416
403
417
@pytest .hookimpl (hookwrapper = True )
404
418
def pytest_sessionfinish (self , exitstatus ):
405
419
outcome = yield
406
420
outcome .get_result ()
407
- self ._tw .line ("" )
421
+ self .writer .line ("" )
408
422
summary_exit_codes = (
409
423
EXIT_OK , EXIT_TESTSFAILED , EXIT_INTERRUPTED , EXIT_USAGEERROR ,
410
424
EXIT_NOTESTSCOLLECTED )
@@ -434,10 +448,10 @@ def _report_keyboardinterrupt(self):
434
448
self .write_sep ("!" , msg )
435
449
if "KeyboardInterrupt" in msg :
436
450
if self .config .option .fulltrace :
437
- excrepr .toterminal (self ._tw )
451
+ excrepr .toterminal (self .writer )
438
452
else :
439
- self ._tw .line ("to show a full traceback on KeyboardInterrupt use --fulltrace" , yellow = True )
440
- excrepr .reprcrash .toterminal (self ._tw )
453
+ self .writer .line ("to show a full traceback on KeyboardInterrupt use --fulltrace" , yellow = True )
454
+ excrepr .reprcrash .toterminal (self .writer )
441
455
442
456
def _locationline (self , nodeid , fspath , lineno , domain ):
443
457
def mkrel (nodeid ):
@@ -493,14 +507,14 @@ def summary_warnings(self):
493
507
grouped = itertools .groupby (all_warnings , key = lambda wr : wr .get_location (self .config ))
494
508
495
509
self .write_sep ("=" , "warnings summary" , yellow = True , bold = False )
496
- for location , warnings in grouped :
497
- self ._tw .line (str (location ) or '<undetermined location>' )
498
- for w in warnings :
510
+ for location , warning_records in grouped :
511
+ self .writer .line (str (location ) or '<undetermined location>' )
512
+ for w in warning_records :
499
513
lines = w .message .splitlines ()
500
514
indented = '\n ' .join (' ' + x for x in lines )
501
- self ._tw .line (indented )
502
- self ._tw .line ()
503
- self ._tw .line ('-- Docs: http://doc.pytest.org/en/latest/warnings.html' )
515
+ self .writer .line (indented )
516
+ self .writer .line ()
517
+ self .writer .line ('-- Docs: http://doc.pytest.org/en/latest/warnings.html' )
504
518
505
519
def summary_passes (self ):
506
520
if self .config .option .tbstyle != "no" :
@@ -517,10 +531,10 @@ def summary_passes(self):
517
531
def print_teardown_sections (self , rep ):
518
532
for secname , content in rep .sections :
519
533
if 'teardown' in secname :
520
- self ._tw .sep ('-' , secname )
534
+ self .writer .sep ('-' , secname )
521
535
if content [- 1 :] == "\n " :
522
536
content = content [:- 1 ]
523
- self ._tw .line (content )
537
+ self .writer .line (content )
524
538
525
539
def summary_failures (self ):
526
540
if self .config .option .tbstyle != "no" :
@@ -560,12 +574,12 @@ def summary_errors(self):
560
574
self ._outrep_summary (rep )
561
575
562
576
def _outrep_summary (self , rep ):
563
- rep .toterminal (self ._tw )
577
+ rep .toterminal (self .writer )
564
578
for secname , content in rep .sections :
565
- self ._tw .sep ("-" , secname )
579
+ self .writer .sep ("-" , secname )
566
580
if content [- 1 :] == "\n " :
567
581
content = content [:- 1 ]
568
- self ._tw .line (content )
582
+ self .writer .line (content )
569
583
570
584
def summary_stats (self ):
571
585
session_duration = time .time () - self ._sessionstarttime
0 commit comments