@@ -157,6 +157,7 @@ def __init__(self, outcome, report, logfile, config):
157157 if getattr (report , "when" , "call" ) != "call" :
158158 self .test_id = "::" .join ([report .nodeid , report .when ])
159159 self .time = getattr (report , "duration" , 0.0 )
160+ self .formatted_time = getattr (report , "formatted_duration" , 0.0 )
160161 self .outcome = outcome
161162 self .additional_html = []
162163 self .links_html = []
@@ -183,7 +184,7 @@ def __init__(self, outcome, report, logfile, config):
183184 cells = [
184185 html .td (self .outcome , class_ = "col-result" ),
185186 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" ),
187188 html .td (self .links_html , class_ = "col-links" ),
188189 ]
189190
@@ -537,7 +538,7 @@ def generate_summary_item(self):
537538 cells = [
538539 html .th ("Result" , class_ = "sortable result initial-sort" , col = "result" ),
539540 html .th ("Test" , class_ = "sortable" , col = "name" ),
540- html .th ("Duration" , class_ = "sortable numeric " , col = "duration" ),
541+ html .th ("Duration" , class_ = "sortable" , col = "duration" ),
541542 html .th ("Links" , class_ = "sortable links" , col = "links" ),
542543 ]
543544 session .config .hook .pytest_html_results_table_header (cells = cells )
@@ -603,6 +604,32 @@ def generate_summary_item(self):
603604 unicode_doc = unicode_doc .encode ("utf-8" , errors = "xmlcharrefreplace" )
604605 return unicode_doc .decode ("utf-8" )
605606
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+
606633 def _generate_environment (self , config ):
607634 if not hasattr (config , "_metadata" ) or config ._metadata is None :
608635 return []
@@ -688,6 +715,7 @@ def _post_process_reports(self):
688715 test_report .longrepr = full_text
689716 test_report .extra = extras
690717 test_report .duration = duration
718+ test_report .formatted_duration = self ._format_duration (test_report )
691719
692720 if wasxfail :
693721 test_report .wasxfail = True
0 commit comments