3
3
import datetime
4
4
import json
5
5
import os
6
- import pytest
7
6
import re
8
7
import warnings
9
-
10
8
from collections import defaultdict
11
9
from functools import partial
12
10
from pathlib import Path
11
+
12
+ import pytest
13
13
from jinja2 import Environment
14
14
from jinja2 import FileSystemLoader
15
15
from jinja2 import select_autoescape
22
22
try :
23
23
from ansi2html import Ansi2HTMLConverter , style
24
24
25
- converter = Ansi2HTMLConverter (
26
- inline = False , escaped = False
27
- )
25
+ converter = Ansi2HTMLConverter (inline = False , escaped = False )
28
26
_handle_ansi = partial (converter .convert , full = False )
29
27
_ansi_styles = style .get_styles ()
30
28
except ImportError :
34
32
_ansi_styles = []
35
33
36
34
37
- class BaseReport ( object ) :
38
- class Cells ( object ) :
35
+ class BaseReport :
36
+ class Cells :
39
37
def __init__ (self ):
40
38
self ._html = {}
41
39
@@ -46,7 +44,7 @@ def html(self):
46
44
def insert (self , index , html ):
47
45
self ._html [index ] = html
48
46
49
- class Report ( object ) :
47
+ class Report :
50
48
def __init__ (self , title , duration_format ):
51
49
self ._data = {
52
50
"title" : title ,
@@ -80,9 +78,13 @@ def __init__(self, report_path, config, default_css="style.css"):
80
78
self ._resources_path = Path (__file__ ).parent .joinpath ("resources" )
81
79
self ._config = config
82
80
self ._template = _read_template ([self ._resources_path ])
83
- self ._css = _process_css (Path (self ._resources_path , default_css ), self ._config .getoption ("css" ))
81
+ self ._css = _process_css (
82
+ Path (self ._resources_path , default_css ), self ._config .getoption ("css" )
83
+ )
84
84
self ._duration_format = config .getini ("duration_format" )
85
- self ._max_asset_filename_length = int (config .getini ("max_asset_filename_length" ))
85
+ self ._max_asset_filename_length = int (
86
+ config .getini ("max_asset_filename_length" )
87
+ )
86
88
self ._report = self .Report (self ._report_path .name , self ._duration_format )
87
89
88
90
@property
@@ -96,7 +98,7 @@ def _asset_filename(self, test_id, extra_index, test_index, file_extension):
96
98
str (extra_index ),
97
99
str (test_index ),
98
100
file_extension ,
99
- )[- self ._max_asset_filename_length :]
101
+ )[- self ._max_asset_filename_length :]
100
102
101
103
def _generate_report (self , self_contained = False ):
102
104
generated = datetime .datetime .now ()
@@ -147,19 +149,28 @@ def _process_extras(self, report, test_id):
147
149
test_id .encode ("utf-8" ).decode ("unicode_escape" ),
148
150
extra_index ,
149
151
test_index ,
150
- extra [' extension' ]
152
+ extra [" extension" ],
151
153
)
152
154
if extra ["format_type" ] == extras .FORMAT_JSON :
153
155
content = json .dumps (content )
154
- extra ["content" ] = self ._data_content (content , asset_name = asset_name , mime_type = extra ["mime_type" ])
156
+ extra ["content" ] = self ._data_content (
157
+ content , asset_name = asset_name , mime_type = extra ["mime_type" ]
158
+ )
155
159
156
160
if extra ["format_type" ] == extras .FORMAT_TEXT :
157
161
if isinstance (content , bytes ):
158
162
content = content .decode ("utf-8" )
159
- extra ["content" ] = self ._data_content (content , asset_name = asset_name , mime_type = extra ["mime_type" ])
160
-
161
- if extra ["format_type" ] == extras .FORMAT_IMAGE or extra ["format_type" ] == extras .FORMAT_VIDEO :
162
- extra ["content" ] = self ._media_content (content , asset_name = asset_name , mime_type = extra ["mime_type" ])
163
+ extra ["content" ] = self ._data_content (
164
+ content , asset_name = asset_name , mime_type = extra ["mime_type" ]
165
+ )
166
+
167
+ if (
168
+ extra ["format_type" ] == extras .FORMAT_IMAGE
169
+ or extra ["format_type" ] == extras .FORMAT_VIDEO
170
+ ):
171
+ extra ["content" ] = self ._media_content (
172
+ content , asset_name = asset_name , mime_type = extra ["mime_type" ]
173
+ )
163
174
164
175
return report_extras
165
176
@@ -218,7 +229,9 @@ def pytest_sessionfinish(self, session):
218
229
219
230
@pytest .hookimpl (trylast = True )
220
231
def pytest_terminal_summary (self , terminalreporter ):
221
- terminalreporter .write_sep ("-" , f"Generated html report: file://{ self ._report_path .resolve ()} " )
232
+ terminalreporter .write_sep (
233
+ "-" , f"Generated html report: file://{ self ._report_path .resolve ()} "
234
+ )
222
235
223
236
@pytest .hookimpl (trylast = True )
224
237
def pytest_collection_finish (self , session ):
@@ -307,7 +320,7 @@ def _media_content(self, content, mime_type, *args, **kwargs):
307
320
base64 .b64decode (content .encode ("utf-8" ), validate = True )
308
321
return f"data:{ mime_type } ;base64,{ content } "
309
322
except binascii .Error :
310
- # if not base64 content , issue warning and just return as it's a file or link
323
+ # if not base64, issue warning and just return as it's a file or link
311
324
warnings .warn (
312
325
"Self-contained HTML report "
313
326
"includes link to external "
@@ -361,7 +374,7 @@ def _read_template(search_paths, template_name="index.jinja2"):
361
374
env = Environment (
362
375
loader = FileSystemLoader (search_paths ),
363
376
autoescape = select_autoescape (
364
- enabled_extensions = (' jinja2' ,),
377
+ enabled_extensions = (" jinja2" ,),
365
378
),
366
379
)
367
380
return env .get_template (template_name )
0 commit comments