Skip to content

Commit 232b713

Browse files
authored
📝 Docs: 添加 pytest-asyncio 配置 (#3136)
1 parent 980affd commit 232b713

File tree

5 files changed

+35
-0
lines changed

5 files changed

+35
-0
lines changed

website/docs/best-practice/testing/README.mdx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,16 @@ asyncio_default_fixture_loop_scope = "session"
8989
```python title=tests/conftest.py
9090
import pytest
9191
import nonebot
92+
from pytest_asyncio import is_async_test
9293
# 导入适配器
9394
from nonebot.adapters.console import Adapter as ConsoleAdapter
9495

96+
def pytest_collection_modifyitems(items: list[pytest.Item]):
97+
pytest_asyncio_tests = (item for item in items if is_async_test(item))
98+
session_scope_marker = pytest.mark.asyncio(loop_scope="session")
99+
for async_test in pytest_asyncio_tests:
100+
async_test.add_marker(session_scope_marker, append=False)
101+
95102
@pytest.fixture(scope="session", autouse=True)
96103
async def after_nonebot_init(after_nonebot_init: None):
97104
# 加载适配器

website/versioned_docs/version-2.3.1/best-practice/testing/README.mdx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,16 @@ asyncio_default_fixture_loop_scope = "session"
8989
```python title=tests/conftest.py
9090
import pytest
9191
import nonebot
92+
from pytest_asyncio import is_async_test
9293
# 导入适配器
9394
from nonebot.adapters.console import Adapter as ConsoleAdapter
9495

96+
def pytest_collection_modifyitems(items: list[pytest.Item]):
97+
pytest_asyncio_tests = (item for item in items if is_async_test(item))
98+
session_scope_marker = pytest.mark.asyncio(loop_scope="session")
99+
for async_test in pytest_asyncio_tests:
100+
async_test.add_marker(session_scope_marker, append=False)
101+
95102
@pytest.fixture(scope="session", autouse=True)
96103
async def after_nonebot_init(after_nonebot_init: None):
97104
# 加载适配器

website/versioned_docs/version-2.3.2/best-practice/testing/README.mdx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,16 @@ asyncio_default_fixture_loop_scope = "session"
8989
```python title=tests/conftest.py
9090
import pytest
9191
import nonebot
92+
from pytest_asyncio import is_async_test
9293
# 导入适配器
9394
from nonebot.adapters.console import Adapter as ConsoleAdapter
9495

96+
def pytest_collection_modifyitems(items: list[pytest.Item]):
97+
pytest_asyncio_tests = (item for item in items if is_async_test(item))
98+
session_scope_marker = pytest.mark.asyncio(loop_scope="session")
99+
for async_test in pytest_asyncio_tests:
100+
async_test.add_marker(session_scope_marker, append=False)
101+
95102
@pytest.fixture(scope="session", autouse=True)
96103
async def after_nonebot_init(after_nonebot_init: None):
97104
# 加载适配器

website/versioned_docs/version-2.3.3/best-practice/testing/README.mdx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,16 @@ asyncio_default_fixture_loop_scope = "session"
8989
```python title=tests/conftest.py
9090
import pytest
9191
import nonebot
92+
from pytest_asyncio import is_async_test
9293
# 导入适配器
9394
from nonebot.adapters.console import Adapter as ConsoleAdapter
9495

96+
def pytest_collection_modifyitems(items: list[pytest.Item]):
97+
pytest_asyncio_tests = (item for item in items if is_async_test(item))
98+
session_scope_marker = pytest.mark.asyncio(loop_scope="session")
99+
for async_test in pytest_asyncio_tests:
100+
async_test.add_marker(session_scope_marker, append=False)
101+
95102
@pytest.fixture(scope="session", autouse=True)
96103
async def after_nonebot_init(after_nonebot_init: None):
97104
# 加载适配器

website/versioned_docs/version-2.4.0/best-practice/testing/README.mdx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,16 @@ asyncio_default_fixture_loop_scope = "session"
8989
```python title=tests/conftest.py
9090
import pytest
9191
import nonebot
92+
from pytest_asyncio import is_async_test
9293
# 导入适配器
9394
from nonebot.adapters.console import Adapter as ConsoleAdapter
9495

96+
def pytest_collection_modifyitems(items: list[pytest.Item]):
97+
pytest_asyncio_tests = (item for item in items if is_async_test(item))
98+
session_scope_marker = pytest.mark.asyncio(loop_scope="session")
99+
for async_test in pytest_asyncio_tests:
100+
async_test.add_marker(session_scope_marker, append=False)
101+
95102
@pytest.fixture(scope="session", autouse=True)
96103
async def after_nonebot_init(after_nonebot_init: None):
97104
# 加载适配器

0 commit comments

Comments
 (0)