diff --git a/src/_pytest/logging.py b/src/_pytest/logging.py index eca5c240b..39523d0a3 100644 --- a/src/_pytest/logging.py +++ b/src/_pytest/logging.py @@ -678,10 +678,6 @@ class LoggingPlugin: with catching_logs(self.log_file_handler, level=self.log_file_level): yield - # Close the FileHandler explicitly. - # (logging.shutdown might have lost the weakref?!) - self.log_file_handler.close() - @pytest.hookimpl(hookwrapper=True, tryfirst=True) def pytest_sessionstart(self): self.log_cli_handler.set_when("sessionstart") @@ -706,6 +702,12 @@ class LoggingPlugin: with catching_logs(self.log_file_handler, level=self.log_file_level): yield # run all the tests + @pytest.hookimpl + def pytest_unconfigure(self): + # Close the FileHandler explicitly. + # (logging.shutdown might have lost the weakref?!) + self.log_file_handler.close() + class _LiveLoggingStreamHandler(logging.StreamHandler): """