@@ -157,6 +157,7 @@ def __init__(self, outcome, report, logfile, config):
157
157
if getattr (report , "when" , "call" ) != "call" :
158
158
self .test_id = "::" .join ([report .nodeid , report .when ])
159
159
self .time = getattr (report , "duration" , 0.0 )
160
+ self .formatted_time = getattr (report , "formatted_duration" , 0.0 )
160
161
self .outcome = outcome
161
162
self .additional_html = []
162
163
self .links_html = []
@@ -183,7 +184,7 @@ def __init__(self, outcome, report, logfile, config):
183
184
cells = [
184
185
html .td (self .outcome , class_ = "col-result" ),
185
186
html .td (self .test_id , class_ = "col-name" ),
186
- html .td (f" { self .time :.2f } " , class_ = "col-duration" ),
187
+ html .td (self .formatted_time , class_ = "col-duration" ),
187
188
html .td (self .links_html , class_ = "col-links" ),
188
189
]
189
190
@@ -537,7 +538,7 @@ def generate_summary_item(self):
537
538
cells = [
538
539
html .th ("Result" , class_ = "sortable result initial-sort" , col = "result" ),
539
540
html .th ("Test" , class_ = "sortable" , col = "name" ),
540
- html .th ("Duration" , class_ = "sortable numeric " , col = "duration" ),
541
+ html .th ("Duration" , class_ = "sortable" , col = "duration" ),
541
542
html .th ("Links" , class_ = "sortable links" , col = "links" ),
542
543
]
543
544
session .config .hook .pytest_html_results_table_header (cells = cells )
@@ -603,6 +604,32 @@ def generate_summary_item(self):
603
604
unicode_doc = unicode_doc .encode ("utf-8" , errors = "xmlcharrefreplace" )
604
605
return unicode_doc .decode ("utf-8" )
605
606
607
+ def _format_duration (self , report ):
608
+ # parse the report duration into its display version and return it to the caller
609
+ duration_formatter = getattr (report , "duration_formatter" , None )
610
+ string_duration = str (report .duration )
611
+ if duration_formatter is None :
612
+ if "." in string_duration :
613
+ split_duration = string_duration .split ("." )
614
+ split_duration [1 ] = split_duration [1 ][0 :2 ]
615
+
616
+ string_duration = "." .join (split_duration )
617
+
618
+ return string_duration
619
+ else :
620
+ # support %f, since time.strftime doesn't support it out of the box
621
+ # keep a precision of 2 for legacy reasons
622
+ formatted_milliseconds = "00"
623
+ if "." in string_duration :
624
+ milliseconds = string_duration .split ("." )[1 ]
625
+ formatted_milliseconds = milliseconds [0 :2 ]
626
+
627
+ duration_formatter = duration_formatter .replace (
628
+ "%f" , formatted_milliseconds
629
+ )
630
+ duration_as_gmtime = time .gmtime (report .duration )
631
+ return time .strftime (duration_formatter , duration_as_gmtime )
632
+
606
633
def _generate_environment (self , config ):
607
634
if not hasattr (config , "_metadata" ) or config ._metadata is None :
608
635
return []
@@ -688,6 +715,7 @@ def _post_process_reports(self):
688
715
test_report .longrepr = full_text
689
716
test_report .extra = extras
690
717
test_report .duration = duration
718
+ test_report .formatted_duration = self ._format_duration (test_report )
691
719
692
720
if wasxfail :
693
721
test_report .wasxfail = True
0 commit comments