33# file, You can obtain one at http://mozilla.org/MPL/2.0/.
44import datetime
55import json
6+ import math
67import os
78import re
89import warnings
1213
1314from pytest_html import __version__
1415from pytest_html import extras
15- from pytest_html .table import Header
16- from pytest_html .table import Row
1716from pytest_html .util import cleanup_unserializable
1817
1918
@@ -60,8 +59,8 @@ def _generate_report(self, self_contained=False):
6059
6160 self ._write_report (rendered_report )
6261
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
6564 for key in metadata .keys ():
6665 value = metadata [key ]
6766 if self ._is_redactable_environment_variable (key ):
@@ -145,16 +144,16 @@ def _write_report(self, rendered_report):
145144
146145 @pytest .hookimpl (trylast = True )
147146 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 ))
151152
152153 session .config .hook .pytest_html_report_title (report = self ._report )
153154
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 )
158157
159158 self ._report .set_data ("runningState" , "Started" )
160159 self ._generate_report ()
@@ -173,7 +172,8 @@ def pytest_sessionfinish(self, session):
173172 @pytest .hookimpl (trylast = True )
174173 def pytest_terminal_summary (self , terminalreporter ):
175174 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 ()} " ,
177177 )
178178
179179 @pytest .hookimpl (trylast = True )
@@ -188,35 +188,58 @@ def pytest_runtest_logreport(self, report):
188188 DeprecationWarning ,
189189 )
190190
191+ data = dict ()
192+ cells = list ()
193+
194+ result = _process_outcome (report )
191195 data = {
192- "duration " : report . duration ,
196+ "result " : result ,
193197 }
198+ cells .append (f'<td class="col-result">{ result } </td>' )
194199
195200 test_id = report .nodeid
196201 if report .when != "call" :
197202 test_id += f"::{ report .when } "
198203 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" ])
199209
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 :
203216 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 )
207220
208221 processed_logs = _process_logs (report )
209222 self ._config .hook .pytest_html_results_table_html (
210223 report = report , data = processed_logs
211224 )
212225
213- data ["result" ] = _process_outcome (report )
214- data ["extras" ] = self ._process_extras (report , test_id )
215-
216226 if self ._report .add_test (data , report , processed_logs ):
217227 self ._generate_report ()
218228
219229
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+
220243def _is_error (report ):
221244 return report .when in ["setup" , "teardown" ] and report .outcome == "failed"
222245
0 commit comments