From 232b7134f0746a15d3e7ac5fd7a2697e2a5d9b37 Mon Sep 17 00:00:00 2001 From: Ju4tCode <42488585+yanyongyu@users.noreply.github.com> Date: Sat, 23 Nov 2024 12:29:12 +0800 Subject: [PATCH] =?UTF-8?q?:memo:=20Docs:=20=E6=B7=BB=E5=8A=A0=20pytest-as?= =?UTF-8?q?yncio=20=E9=85=8D=E7=BD=AE=20(#3136)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- website/docs/best-practice/testing/README.mdx | 7 +++++++ .../version-2.3.1/best-practice/testing/README.mdx | 7 +++++++ .../version-2.3.2/best-practice/testing/README.mdx | 7 +++++++ .../version-2.3.3/best-practice/testing/README.mdx | 7 +++++++ .../version-2.4.0/best-practice/testing/README.mdx | 7 +++++++ 5 files changed, 35 insertions(+) diff --git a/website/docs/best-practice/testing/README.mdx b/website/docs/best-practice/testing/README.mdx index bba3012d..12853d96 100644 --- a/website/docs/best-practice/testing/README.mdx +++ b/website/docs/best-practice/testing/README.mdx @@ -89,9 +89,16 @@ asyncio_default_fixture_loop_scope = "session" ```python title=tests/conftest.py import pytest import nonebot +from pytest_asyncio import is_async_test # 导入适配器 from nonebot.adapters.console import Adapter as ConsoleAdapter +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) + @pytest.fixture(scope="session", autouse=True) async def after_nonebot_init(after_nonebot_init: None): # 加载适配器 diff --git a/website/versioned_docs/version-2.3.1/best-practice/testing/README.mdx b/website/versioned_docs/version-2.3.1/best-practice/testing/README.mdx index bba3012d..12853d96 100644 --- a/website/versioned_docs/version-2.3.1/best-practice/testing/README.mdx +++ b/website/versioned_docs/version-2.3.1/best-practice/testing/README.mdx @@ -89,9 +89,16 @@ asyncio_default_fixture_loop_scope = "session" ```python title=tests/conftest.py import pytest import nonebot +from pytest_asyncio import is_async_test # 导入适配器 from nonebot.adapters.console import Adapter as ConsoleAdapter +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) + @pytest.fixture(scope="session", autouse=True) async def after_nonebot_init(after_nonebot_init: None): # 加载适配器 diff --git a/website/versioned_docs/version-2.3.2/best-practice/testing/README.mdx b/website/versioned_docs/version-2.3.2/best-practice/testing/README.mdx index bba3012d..12853d96 100644 --- a/website/versioned_docs/version-2.3.2/best-practice/testing/README.mdx +++ b/website/versioned_docs/version-2.3.2/best-practice/testing/README.mdx @@ -89,9 +89,16 @@ asyncio_default_fixture_loop_scope = "session" ```python title=tests/conftest.py import pytest import nonebot +from pytest_asyncio import is_async_test # 导入适配器 from nonebot.adapters.console import Adapter as ConsoleAdapter +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) + @pytest.fixture(scope="session", autouse=True) async def after_nonebot_init(after_nonebot_init: None): # 加载适配器 diff --git a/website/versioned_docs/version-2.3.3/best-practice/testing/README.mdx b/website/versioned_docs/version-2.3.3/best-practice/testing/README.mdx index bba3012d..12853d96 100644 --- a/website/versioned_docs/version-2.3.3/best-practice/testing/README.mdx +++ b/website/versioned_docs/version-2.3.3/best-practice/testing/README.mdx @@ -89,9 +89,16 @@ asyncio_default_fixture_loop_scope = "session" ```python title=tests/conftest.py import pytest import nonebot +from pytest_asyncio import is_async_test # 导入适配器 from nonebot.adapters.console import Adapter as ConsoleAdapter +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) + @pytest.fixture(scope="session", autouse=True) async def after_nonebot_init(after_nonebot_init: None): # 加载适配器 diff --git a/website/versioned_docs/version-2.4.0/best-practice/testing/README.mdx b/website/versioned_docs/version-2.4.0/best-practice/testing/README.mdx index bba3012d..12853d96 100644 --- a/website/versioned_docs/version-2.4.0/best-practice/testing/README.mdx +++ b/website/versioned_docs/version-2.4.0/best-practice/testing/README.mdx @@ -89,9 +89,16 @@ asyncio_default_fixture_loop_scope = "session" ```python title=tests/conftest.py import pytest import nonebot +from pytest_asyncio import is_async_test # 导入适配器 from nonebot.adapters.console import Adapter as ConsoleAdapter +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) + @pytest.fixture(scope="session", autouse=True) async def after_nonebot_init(after_nonebot_init: None): # 加载适配器