From fd47a688655f282c7fa681914b006f2e19787ea4 Mon Sep 17 00:00:00 2001 From: BalconyJH Date: Sat, 21 Dec 2024 20:59:18 +0800 Subject: [PATCH] :white_check_mark: Add pytest hook to tag async tests with session-scoped event loop. --- tests/conftest.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/conftest.py b/tests/conftest.py index a1b7bf2f..cf29b754 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -8,6 +8,7 @@ from nonebug import NONEBOT_INIT_KWARGS from nonebug.app import App from nonebug.mixin.process import MatcherContext import pytest +from pytest_asyncio import is_async_test from pytest_mock import MockerFixture from respx import MockRouter @@ -22,6 +23,13 @@ def get_response_json(path: str) -> dict: ) +def pytest_collection_modifyitems(items: list[pytest.Item]): + pytest_asyncio_tests = (item for item in items if is_async_test(item)) + session_scope_marker = pytest.mark.asyncio(loop_scope="session") + for async_test in pytest_asyncio_tests: + async_test.add_marker(session_scope_marker, append=False) + + def pytest_configure(config: pytest.Config) -> None: config.stash[NONEBOT_INIT_KWARGS] = { "driver": "~fastapi+~httpx+~websockets",