Add pytest hook to tag async tests with session-scoped event loop.

This commit is contained in:
BalconyJH 2024-12-21 20:59:18 +08:00
parent d6b84a015f
commit fd47a68865
No known key found for this signature in database
GPG Key ID: FF602923BD2A1FAF

View File

@ -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",