2
2
# License, v. 2.0. If a copy of the MPL was not distributed with this
3
3
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
4
4
5
- from __future__ import absolute_import
6
-
7
5
from base64 import b64encode , b64decode
8
6
from collections import OrderedDict
9
7
from os .path import isfile
10
8
import datetime
11
9
import json
12
10
import os
13
11
import pkg_resources
14
- import sys
15
12
import time
16
13
import bisect
17
14
import warnings
18
15
import re
19
16
17
+ from html import escape
18
+
20
19
try :
21
20
from ansi2html import Ansi2HTMLConverter , style
22
21
30
29
from . import extras
31
30
from . import __version__ , __pypi_url__
32
31
33
- PY3 = sys .version_info [0 ] == 3
34
-
35
- # Python 2.X and 3.X compatibility
36
- if PY3 :
37
- basestring = str
38
- from html import escape
39
- else :
40
- from codecs import open
41
- from cgi import escape
42
-
43
32
44
33
def pytest_addhooks (pluginmanager ):
45
34
from . import hooks
@@ -95,10 +84,10 @@ def pytest_unconfigure(config):
95
84
96
85
def data_uri (content , mime_type = "text/plain" , charset = "utf-8" ):
97
86
data = b64encode (content .encode (charset )).decode ("ascii" )
98
- return "data:{0 };charset={1 };base64,{2}" . format ( mime_type , charset , data )
87
+ return f "data:{ mime_type } ;charset={ charset } ;base64,{ data } "
99
88
100
89
101
- class HTMLReport ( object ) :
90
+ class HTMLReport :
102
91
def __init__ (self , logfile , config ):
103
92
logfile = os .path .expanduser (os .path .expandvars (logfile ))
104
93
self .logfile = os .path .abspath (logfile )
@@ -136,7 +125,7 @@ def __init__(self, outcome, report, logfile, config):
136
125
cells = [
137
126
html .td (self .outcome , class_ = "col-result" ),
138
127
html .td (self .test_id , class_ = "col-name" ),
139
- html .td ("{0 :.2f}". format ( self . time ) , class_ = "col-duration" ),
128
+ html .td (f" { self . time :.2f} " , class_ = "col-duration" ),
140
129
html .td (self .links_html , class_ = "col-links" ),
141
130
]
142
131
@@ -181,7 +170,7 @@ def create_asset(
181
170
if not os .path .exists (os .path .dirname (asset_path )):
182
171
os .makedirs (os .path .dirname (asset_path ))
183
172
184
- relative_path = "{0}/{1}" . format ( "assets" , asset_file_name )
173
+ relative_path = f"assets/ { asset_file_name } "
185
174
186
175
kwargs = {"encoding" : "utf-8" } if "b" not in mode else {}
187
176
with open (asset_path , mode , ** kwargs ) as f :
@@ -209,13 +198,10 @@ def append_extra_html(self, extra, extra_index, test_index):
209
198
)
210
199
html_div = html .a (html .img (src = content ), href = content )
211
200
elif self .self_contained :
212
- src = "data:{0 };base64,{1 }" .format (extra .get ("mime_type" ), content )
201
+ src = "data:{};base64,{}" .format (extra .get ("mime_type" ), content )
213
202
html_div = html .img (src = src )
214
203
else :
215
- if PY3 :
216
- content = content .encode ("utf-8" )
217
-
218
- content = b64decode (content )
204
+ content = b64decode (content .encode ("utf-8" ))
219
205
href = src = self .create_asset (
220
206
content , extra_index , test_index , extra .get ("extension" ), "wb"
221
207
)
@@ -276,7 +262,7 @@ def append_log_html(self, report, additional_html):
276
262
277
263
for section in report .sections :
278
264
header , content = map (escape , section )
279
- log .append (" {0} " . format ( header ). center ( 80 , "-" ) )
265
+ log .append (f " { header :-^80 } " )
280
266
log .append (html .br ())
281
267
if ANSI :
282
268
converter = Ansi2HTMLConverter (inline = False , escaped = False )
@@ -296,7 +282,7 @@ def _appendrow(self, outcome, report):
296
282
self .results .insert (index , result )
297
283
tbody = html .tbody (
298
284
result .row_table ,
299
- class_ = "{0 } results-table-row" .format (result .outcome .lower ()),
285
+ class_ = "{} results-table-row" .format (result .outcome .lower ()),
300
286
)
301
287
if result .row_extra is not None :
302
288
tbody .append (result .row_extra )
@@ -345,9 +331,7 @@ def _generate_report(self, session):
345
331
346
332
self .style_css = pkg_resources .resource_string (
347
333
__name__ , os .path .join ("resources" , "style.css" )
348
- )
349
- if PY3 :
350
- self .style_css = self .style_css .decode ("utf-8" )
334
+ ).decode ("utf-8" )
351
335
352
336
if ANSI :
353
337
ansi_css = [
@@ -362,12 +346,12 @@ def _generate_report(self, session):
362
346
for path in self .config .getoption ("css" ):
363
347
self .style_css += "\n /******************************"
364
348
self .style_css += "\n * CUSTOM CSS"
365
- self .style_css += "\n * {}" . format ( path )
349
+ self .style_css += f "\n * { path } "
366
350
self .style_css += "\n ******************************/\n \n "
367
351
with open (path , "r" ) as f :
368
352
self .style_css += f .read ()
369
353
370
- css_href = "{0}/{1}" . format ( " assets" , " style.css")
354
+ css_href = "assets/ style.css"
371
355
html_css = html .link (href = css_href , rel = "stylesheet" , type = "text/css" )
372
356
if self .self_contained :
373
357
html_css = html .style (raw (self .style_css ))
@@ -401,12 +385,12 @@ def generate_checkbox(self):
401
385
name = "filter_checkbox" ,
402
386
class_ = "filter" ,
403
387
hidden = "true" ,
404
- ** checkbox_kwargs
388
+ ** checkbox_kwargs ,
405
389
)
406
390
407
391
def generate_summary_item (self ):
408
392
self .summary_item = html .span (
409
- "{0} {1}" . format ( self .total , self .label ) , class_ = self .class_html
393
+ f" { self .total } { self .label } " , class_ = self .class_html
410
394
)
411
395
412
396
outcomes = [
@@ -422,9 +406,7 @@ def generate_summary_item(self):
422
406
outcomes .append (Outcome ("rerun" , self .rerun ))
423
407
424
408
summary = [
425
- html .p (
426
- "{0} tests ran in {1:.2f} seconds. " .format (numtests , suite_time_delta )
427
- ),
409
+ html .p (f"{ numtests } tests ran in { suite_time_delta :.2f} seconds. " ),
428
410
html .p (
429
411
"(Un)check the boxes to filter the results." ,
430
412
class_ = "filter" ,
@@ -472,19 +454,17 @@ def generate_summary_item(self):
472
454
473
455
main_js = pkg_resources .resource_string (
474
456
__name__ , os .path .join ("resources" , "main.js" )
475
- )
476
- if PY3 :
477
- main_js = main_js .decode ("utf-8" )
457
+ ).decode ("utf-8" )
478
458
479
459
body = html .body (
480
460
html .script (raw (main_js )),
481
461
html .h1 (os .path .basename (self .logfile )),
482
462
html .p (
483
- "Report generated on {0 } at {1 } by " .format (
463
+ "Report generated on {} at {} by " .format (
484
464
generated .strftime ("%d-%b-%Y" ), generated .strftime ("%H:%M:%S" )
485
465
),
486
466
html .a ("pytest-html" , href = __pypi_url__ ),
487
- " v{0}" . format ( __version__ ) ,
467
+ f " v{ __version__ } " ,
488
468
),
489
469
onLoad = "init()" ,
490
470
)
@@ -501,12 +481,11 @@ def generate_summary_item(self):
501
481
502
482
doc = html .html (head , body )
503
483
504
- unicode_doc = u"<!DOCTYPE html>\n {0}" .format (doc .unicode (indent = 2 ))
505
- if PY3 :
506
- # Fix encoding issues, e.g. with surrogates
507
- unicode_doc = unicode_doc .encode ("utf-8" , errors = "xmlcharrefreplace" )
508
- unicode_doc = unicode_doc .decode ("utf-8" )
509
- return unicode_doc
484
+ unicode_doc = "<!DOCTYPE html>\n {}" .format (doc .unicode (indent = 2 ))
485
+
486
+ # Fix encoding issues, e.g. with surrogates
487
+ unicode_doc = unicode_doc .encode ("utf-8" , errors = "xmlcharrefreplace" )
488
+ return unicode_doc .decode ("utf-8" )
510
489
511
490
def _generate_environment (self , config ):
512
491
if not hasattr (config , "_metadata" ) or config ._metadata is None :
@@ -522,10 +501,10 @@ def _generate_environment(self, config):
522
501
523
502
for key in keys :
524
503
value = metadata [key ]
525
- if isinstance (value , basestring ) and value .startswith ("http" ):
504
+ if isinstance (value , str ) and value .startswith ("http" ):
526
505
value = html .a (value , href = value , target = "_blank" )
527
506
elif isinstance (value , (list , tuple , set )):
528
- value = ", " .join (( str (i ) for i in value ) )
507
+ value = ", " .join (str (i ) for i in value )
529
508
rows .append (html .tr (html .td (key ), html .td (value )))
530
509
531
510
environment .append (html .table (rows , id = "environment" ))
@@ -569,6 +548,4 @@ def pytest_sessionfinish(self, session):
569
548
self ._save_report (report_content )
570
549
571
550
def pytest_terminal_summary (self , terminalreporter ):
572
- terminalreporter .write_sep (
573
- "-" , "generated html file: file://{0}" .format (self .logfile )
574
- )
551
+ terminalreporter .write_sep ("-" , f"generated html file: file://{ self .logfile } " )
0 commit comments