3
3
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
4
4
import datetime
5
5
import json
6
+ import math
6
7
import os
7
8
import re
8
9
import warnings
12
13
13
14
from pytest_html import __version__
14
15
from pytest_html import extras
15
- from pytest_html .table import Header
16
- from pytest_html .table import Row
17
16
from pytest_html .util import cleanup_unserializable
18
17
19
18
@@ -60,8 +59,8 @@ def _generate_report(self, self_contained=False):
60
59
61
60
self ._write_report (rendered_report )
62
61
63
- def _generate_environment (self ):
64
- metadata = self ._config ._metadata
62
+ def _generate_environment (self , metadata_key ):
63
+ metadata = self ._config .stash [ metadata_key ] # self._config. _metadata
65
64
for key in metadata .keys ():
66
65
value = metadata [key ]
67
66
if self ._is_redactable_environment_variable (key ):
@@ -145,16 +144,16 @@ def _write_report(self, rendered_report):
145
144
146
145
@pytest .hookimpl (trylast = True )
147
146
def pytest_sessionstart (self , session ):
148
- config = session .config
149
- if hasattr (config , "_metadata" ) and config ._metadata :
150
- self ._report .set_data ("environment" , self ._generate_environment ())
147
+ # config = session.config
148
+ from pytest_metadata .plugin import metadata_key
149
+
150
+ # if hasattr(config, "_metadata") and config._metadata:
151
+ self ._report .set_data ("environment" , self ._generate_environment (metadata_key ))
151
152
152
153
session .config .hook .pytest_html_report_title (report = self ._report )
153
154
154
- header_cells = Header ()
155
- session .config .hook .pytest_html_results_table_header (cells = header_cells )
156
- self ._report .set_data ("resultsTableHeader" , header_cells .html )
157
- self ._report .set_data ("headerPops" , header_cells .get_pops ())
155
+ headers = self ._report .data ["resultsTableHeader" ]
156
+ session .config .hook .pytest_html_results_table_header (cells = headers )
158
157
159
158
self ._report .set_data ("runningState" , "Started" )
160
159
self ._generate_report ()
@@ -173,7 +172,8 @@ def pytest_sessionfinish(self, session):
173
172
@pytest .hookimpl (trylast = True )
174
173
def pytest_terminal_summary (self , terminalreporter ):
175
174
terminalreporter .write_sep (
176
- "-" , f"Generated html report: file://{ self ._report_path .resolve ()} "
175
+ "-" ,
176
+ f"Generated html report: file://{ self ._report_path .resolve ().as_posix ()} " ,
177
177
)
178
178
179
179
@pytest .hookimpl (trylast = True )
@@ -188,35 +188,58 @@ def pytest_runtest_logreport(self, report):
188
188
DeprecationWarning ,
189
189
)
190
190
191
+ data = dict ()
192
+ cells = list ()
193
+
194
+ result = _process_outcome (report )
191
195
data = {
192
- "duration " : report . duration ,
196
+ "result " : result ,
193
197
}
198
+ cells .append (f'<td class="col-result">{ result } </td>' )
194
199
195
200
test_id = report .nodeid
196
201
if report .when != "call" :
197
202
test_id += f"::{ report .when } "
198
203
data ["testId" ] = test_id
204
+ cells .append (f'<td class="col-name">{ test_id } </td>' )
205
+
206
+ total_duration = self ._report .data ["totalDuration" ]
207
+ total_duration ["total" ] += report .duration
208
+ total_duration ["formatted" ] = _format_duration (total_duration ["total" ])
199
209
200
- row_cells = Row ()
201
- self ._config .hook .pytest_html_results_table_row (report = report , cells = row_cells )
202
- if row_cells .html is None :
210
+ duration = _format_duration (report .duration )
211
+ cells .append (f'<td class="col-duration">{ duration } </td>' )
212
+ cells .append ('<td class="col-links"></td>' )
213
+
214
+ self ._config .hook .pytest_html_results_table_row (report = report , cells = cells )
215
+ if not cells :
203
216
return
204
- data [ "resultsTableRow" ] = row_cells . html
205
- for sortable , value in row_cells . sortables . items ():
206
- data [sortable ] = value
217
+
218
+ data [ "resultsTableRow" ] = cells
219
+ data ["extras" ] = self . _process_extras ( report , test_id )
207
220
208
221
processed_logs = _process_logs (report )
209
222
self ._config .hook .pytest_html_results_table_html (
210
223
report = report , data = processed_logs
211
224
)
212
225
213
- data ["result" ] = _process_outcome (report )
214
- data ["extras" ] = self ._process_extras (report , test_id )
215
-
216
226
if self ._report .add_test (data , report , processed_logs ):
217
227
self ._generate_report ()
218
228
219
229
230
+ def _format_duration (duration ):
231
+ if duration < 1 :
232
+ return "{} ms" .format (round (duration * 1000 ))
233
+
234
+ hours = math .floor (duration / 3600 )
235
+ remaining_seconds = duration % 3600
236
+ minutes = math .floor (remaining_seconds / 60 )
237
+ remaining_seconds = remaining_seconds % 60
238
+ seconds = round (remaining_seconds )
239
+
240
+ return f"{ hours :02d} :{ minutes :02d} :{ seconds :02d} "
241
+
242
+
220
243
def _is_error (report ):
221
244
return report .when in ["setup" , "teardown" ] and report .outcome == "failed"
222
245
0 commit comments