diff --git a/pytest.ini b/pytest.ini index b5a6b0c9fd6b..f859895ccbcc 100644 --- a/pytest.ini +++ b/pytest.ini @@ -1,6 +1,5 @@ # pytest.ini file for all the functional tests of the userver drivers. [pytest] -asyncio_mode = auto mockserver-tracing-enabled = true log_level = DEBUG diff --git a/testsuite/pytest_plugins/pytest_userver/plugins/logging.py b/testsuite/pytest_plugins/pytest_userver/plugins/logging.py index f25c2258e88f..7cf6b94243f4 100644 --- a/testsuite/pytest_plugins/pytest_userver/plugins/logging.py +++ b/testsuite/pytest_plugins/pytest_userver/plugins/logging.py @@ -105,19 +105,16 @@ def pytest_sessionfinish(self, session): if self._live_logs: self._live_logs.join() - @pytest.hookimpl(wrapper=True) def pytest_runtest_setup(self, item): self._flushers.clear() self.update_position() - yield from self._userver_log_dump(item, 'setup') - @pytest.hookimpl(wrapper=True) - def pytest_runtest_call(self, item): - yield from self._userver_log_dump(item, 'call') - - @pytest.hookimpl(wrapper=True) - def pytest_runtest_teardown(self, item): - yield from self._userver_log_dump(item, 'teardown') + @pytest.hookimpl(wrapper=True, tryfirst=True) + def pytest_runtest_makereport(self, item, call): + report = yield + if report.failed: + self._userver_report_attach(report) + return report def update_position(self): for logfile in self._logs.values(): @@ -133,32 +130,23 @@ def register_logfile(self, path: pathlib.Path, title: str): if self._live_logs: self._live_logs.register_logfile(path) - def _userver_log_dump(self, item, when): - try: - yield - except Exception: - self._userver_report_attach(item, when) - raise - - def _userver_report_attach(self, item, when): + def _userver_report_attach(self, report): self._run_flushers() for (_, title), logfile in self._logs.items(): - self._userver_report_attach_log(logfile, item, when, title) + self._userver_report_attach_log(logfile, report, title) - def _userver_report_attach_log(self, logfile: LogFile, item, when, title): - report = io.StringIO() + def _userver_report_attach_log(self, logfile: LogFile, report, title): + log = io.StringIO() colorizer = self._colorize_factory() for line in logfile.readlines(): line = line.rstrip('\r\n') line = colorizer(line) if line: - report.write(line) - report.write('\n') - value = report.getvalue() + log.write(line) + log.write('\n') + value = log.getvalue() if value: - item.add_report_section(when, title, value) - - self._run_flushers() + report.sections.append((f'Captured {title} {report.when}', value)) def _run_flushers(self): loop = asyncio.get_event_loop() diff --git a/testsuite/pytest_plugins/pytest_userver/plugins/service.py b/testsuite/pytest_plugins/pytest_userver/plugins/service.py index e9227a648514..e073a34e60e9 100644 --- a/testsuite/pytest_plugins/pytest_userver/plugins/service.py +++ b/testsuite/pytest_plugins/pytest_userver/plugins/service.py @@ -105,7 +105,6 @@ async def service_daemon( service_config, service_binary, service_non_http_health_checks, - testsuite_logger, ): """ Configures the health checking to use service_http_ping_url fixture value diff --git a/testsuite/pytest_plugins/pytest_userver/plugins/service_client.py b/testsuite/pytest_plugins/pytest_userver/plugins/service_client.py index b999eeb5fd93..e3ea0840ee11 100644 --- a/testsuite/pytest_plugins/pytest_userver/plugins/service_client.py +++ b/testsuite/pytest_plugins/pytest_userver/plugins/service_client.py @@ -124,12 +124,15 @@ def userver_client_cleanup(request, _userver_logging_plugin): tasks_to_suspend = () @compat.asynccontextmanager - async def cleanup_manager(client: client.AiohttpClient): + async def cleanup_manager(client: client.Client): @_userver_logging_plugin.register_flusher async def do_flush(): try: await client.log_flush() - except aiohttp.client_exceptions.ClientResponseError: + except aiohttp.client_exceptions.ClientError: + pass + except RuntimeError: + # TODO: find a better way to handle closed aiohttp session pass # Service is already started we don't want startup logs to be shown