@@ -648,39 +648,39 @@ def pytest_sessionfinish(self) -> None:
648
648
dirname = os .path .dirname (os .path .abspath (self .logfile ))
649
649
if not os .path .isdir (dirname ):
650
650
os .makedirs (dirname )
651
- logfile = open ( self . logfile , "w" , encoding = "utf-8" )
652
- suite_stop_time = timing . time ()
653
- suite_time_delta = suite_stop_time - self . suite_start_time
654
-
655
- numtests = (
656
- self . stats [ "passed" ]
657
- + self .stats ["failure " ]
658
- + self .stats ["skipped " ]
659
- + self .stats ["error " ]
660
- - self .cnt_double_fail_tests
661
- )
662
- logfile . write ( '<?xml version="1.0" encoding="utf-8"?>' )
663
-
664
- suite_node = ET . Element (
665
- "testsuite" ,
666
- name = self . suite_name ,
667
- errors = str ( self .stats [ "error" ]) ,
668
- failures = str (self .stats ["failure " ]),
669
- skipped = str (self .stats ["skipped " ]),
670
- tests = str (numtests ),
671
- time = "%.3f" % suite_time_delta ,
672
- timestamp = datetime . fromtimestamp ( self . suite_start_time ). isoformat () ,
673
- hostname = platform . node (),
674
- )
675
- global_properties = self . _get_global_properties_node ( )
676
- if global_properties is not None :
677
- suite_node . append ( global_properties )
678
- for node_reporter in self . node_reporters_ordered :
679
- suite_node . append ( node_reporter . to_xml ())
680
- testsuites = ET . Element ( "testsuites" )
681
- testsuites . append ( suite_node )
682
- logfile . write ( ET . tostring ( testsuites , encoding = "unicode" ) )
683
- logfile .close ( )
651
+
652
+ with open ( self . logfile , "w" , encoding = "utf-8" ) as logfile :
653
+ suite_stop_time = timing . time ()
654
+ suite_time_delta = suite_stop_time - self . suite_start_time
655
+
656
+ numtests = (
657
+ self .stats ["passed " ]
658
+ + self .stats ["failure " ]
659
+ + self .stats ["skipped " ]
660
+ + self .stats [ "error" ]
661
+ - self . cnt_double_fail_tests
662
+ )
663
+ logfile . write ( '<?xml version="1.0" encoding="utf-8"?>' )
664
+
665
+ suite_node = ET . Element (
666
+ "testsuite" ,
667
+ name = self .suite_name ,
668
+ errors = str (self .stats ["error " ]),
669
+ failures = str (self .stats ["failure " ]),
670
+ skipped = str (self . stats [ "skipped" ] ),
671
+ tests = str ( numtests ) ,
672
+ time = "%.3f" % suite_time_delta ,
673
+ timestamp = datetime . fromtimestamp ( self . suite_start_time ). isoformat (),
674
+ hostname = platform . node (),
675
+ )
676
+ global_properties = self . _get_global_properties_node ()
677
+ if global_properties is not None :
678
+ suite_node . append ( global_properties )
679
+ for node_reporter in self . node_reporters_ordered :
680
+ suite_node . append ( node_reporter . to_xml () )
681
+ testsuites = ET . Element ( "testsuites" )
682
+ testsuites . append ( suite_node )
683
+ logfile .write ( ET . tostring ( testsuites , encoding = "unicode" ) )
684
684
685
685
def pytest_terminal_summary (self , terminalreporter : TerminalReporter ) -> None :
686
686
terminalreporter .write_sep ("-" , f"generated xml file: { self .logfile } " )
0 commit comments