logging: inline _runtest_for_main into _runtest_for

This avoids a little bit of overhead, and makes the code a bit clearer
too.
This commit is contained in:
Ran Benita 2020-05-17 14:58:04 +03:00
parent ac6c02f1e2
commit 9effbe7425
1 changed files with 17 additions and 21 deletions

View File

@ -626,17 +626,8 @@ class LoggingPlugin:
yield yield
@contextmanager @contextmanager
def _runtest_for(self, item, when): def _runtest_for(
with self._runtest_for_main(item, when): self, item: Optional[nodes.Item], when: str
if self.log_file_handler is not None:
with catching_logs(self.log_file_handler, level=self.log_file_level):
yield
else:
yield
@contextmanager
def _runtest_for_main(
self, item: nodes.Item, when: str
) -> Generator[None, None, None]: ) -> Generator[None, None, None]:
"""Implements the internals of pytest_runtest_xxx() hook.""" """Implements the internals of pytest_runtest_xxx() hook."""
with catching_logs( with catching_logs(
@ -645,21 +636,26 @@ class LoggingPlugin:
if self.log_cli_handler: if self.log_cli_handler:
self.log_cli_handler.set_when(when) self.log_cli_handler.set_when(when)
if item is None: if item is not None:
yield # run the test
return
empty = {} # type: Dict[str, LogCaptureHandler] empty = {} # type: Dict[str, LogCaptureHandler]
item._store.setdefault(catch_log_handlers_key, empty)[when] = log_handler item._store.setdefault(catch_log_handlers_key, empty)[
when
] = log_handler
item._store[catch_log_handler_key] = log_handler item._store[catch_log_handler_key] = log_handler
try: try:
yield # run test if self.log_file_handler is not None:
with catching_logs(
self.log_file_handler, level=self.log_file_level
):
yield
else:
yield
finally: finally:
if when == "teardown": if item is not None and when == "teardown":
del item._store[catch_log_handlers_key] del item._store[catch_log_handlers_key]
del item._store[catch_log_handler_key] del item._store[catch_log_handler_key]
if self.print_logs: if item is not None and self.print_logs:
# Add a captured log section to the report. # Add a captured log section to the report.
log = log_handler.stream.getvalue().strip() log = log_handler.stream.getvalue().strip()
item.add_report_section(when, "log", log) item.add_report_section(when, "log", log)