From d581823bc533239d46ed669429578d1a6ca5a2b5 Mon Sep 17 00:00:00 2001 From: Ju4tCode <42488585+yanyongyu@users.noreply.github.com> Date: Tue, 29 Oct 2024 14:45:30 +0800 Subject: [PATCH 1/3] :memo: add pytest-asyncio config --- website/docs/best-practice/testing/README.mdx | 11 ++++++++++- .../version-2.3.1/best-practice/testing/README.mdx | 11 ++++++++++- .../version-2.3.2/best-practice/testing/README.mdx | 11 ++++++++++- .../version-2.3.3/best-practice/testing/README.mdx | 11 ++++++++++- 4 files changed, 40 insertions(+), 4 deletions(-) diff --git a/website/docs/best-practice/testing/README.mdx b/website/docs/best-practice/testing/README.mdx index 64f03f9831db..01ff1664e884 100644 --- a/website/docs/best-practice/testing/README.mdx +++ b/website/docs/best-practice/testing/README.mdx @@ -74,7 +74,16 @@ pip install pytest-asyncio ## 配置测试 -在开始测试之前,我们需要对测试进行一些配置,以正确启动我们的机器人。在 `tests` 目录下新建 `conftest.py` 文件,添加以下内容: +在开始测试之前,我们需要对测试进行一些配置,以正确启动我们的机器人。 + +首先我们需要配置 pytest-asyncio,在 `pyproject.toml` 的 pytest 配置部分添加: + +```toml +[tool.pytest.ini_options] +asyncio_mode = "auto" +``` + +然后,我们在 `tests` 目录下新建 `conftest.py` 文件,添加以下内容: ```python title=tests/conftest.py import pytest 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 64f03f9831db..01ff1664e884 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 @@ -74,7 +74,16 @@ pip install pytest-asyncio ## 配置测试 -在开始测试之前,我们需要对测试进行一些配置,以正确启动我们的机器人。在 `tests` 目录下新建 `conftest.py` 文件,添加以下内容: +在开始测试之前,我们需要对测试进行一些配置,以正确启动我们的机器人。 + +首先我们需要配置 pytest-asyncio,在 `pyproject.toml` 的 pytest 配置部分添加: + +```toml +[tool.pytest.ini_options] +asyncio_mode = "auto" +``` + +然后,我们在 `tests` 目录下新建 `conftest.py` 文件,添加以下内容: ```python title=tests/conftest.py import pytest 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 64f03f9831db..01ff1664e884 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 @@ -74,7 +74,16 @@ pip install pytest-asyncio ## 配置测试 -在开始测试之前,我们需要对测试进行一些配置,以正确启动我们的机器人。在 `tests` 目录下新建 `conftest.py` 文件,添加以下内容: +在开始测试之前,我们需要对测试进行一些配置,以正确启动我们的机器人。 + +首先我们需要配置 pytest-asyncio,在 `pyproject.toml` 的 pytest 配置部分添加: + +```toml +[tool.pytest.ini_options] +asyncio_mode = "auto" +``` + +然后,我们在 `tests` 目录下新建 `conftest.py` 文件,添加以下内容: ```python title=tests/conftest.py import pytest 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 64f03f9831db..01ff1664e884 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 @@ -74,7 +74,16 @@ pip install pytest-asyncio ## 配置测试 -在开始测试之前,我们需要对测试进行一些配置,以正确启动我们的机器人。在 `tests` 目录下新建 `conftest.py` 文件,添加以下内容: +在开始测试之前,我们需要对测试进行一些配置,以正确启动我们的机器人。 + +首先我们需要配置 pytest-asyncio,在 `pyproject.toml` 的 pytest 配置部分添加: + +```toml +[tool.pytest.ini_options] +asyncio_mode = "auto" +``` + +然后,我们在 `tests` 目录下新建 `conftest.py` 文件,添加以下内容: ```python title=tests/conftest.py import pytest From 00e60e9cc43b358e9b74a7ce1cbd1d44f2a15be7 Mon Sep 17 00:00:00 2001 From: Ju4tCode <42488585+yanyongyu@users.noreply.github.com> Date: Tue, 29 Oct 2024 15:58:52 +0800 Subject: [PATCH 2/3] :memo: add fixture loop scope config --- website/docs/best-practice/testing/README.mdx | 1 + .../version-2.3.1/best-practice/testing/README.mdx | 1 + .../version-2.3.2/best-practice/testing/README.mdx | 1 + .../version-2.3.3/best-practice/testing/README.mdx | 1 + 4 files changed, 4 insertions(+) diff --git a/website/docs/best-practice/testing/README.mdx b/website/docs/best-practice/testing/README.mdx index 01ff1664e884..f68aa5566deb 100644 --- a/website/docs/best-practice/testing/README.mdx +++ b/website/docs/best-practice/testing/README.mdx @@ -81,6 +81,7 @@ pip install pytest-asyncio ```toml [tool.pytest.ini_options] asyncio_mode = "auto" +asyncio_default_fixture_loop_scope = "session" ``` 然后,我们在 `tests` 目录下新建 `conftest.py` 文件,添加以下内容: 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 01ff1664e884..f68aa5566deb 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 @@ -81,6 +81,7 @@ pip install pytest-asyncio ```toml [tool.pytest.ini_options] asyncio_mode = "auto" +asyncio_default_fixture_loop_scope = "session" ``` 然后,我们在 `tests` 目录下新建 `conftest.py` 文件,添加以下内容: 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 01ff1664e884..f68aa5566deb 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 @@ -81,6 +81,7 @@ pip install pytest-asyncio ```toml [tool.pytest.ini_options] asyncio_mode = "auto" +asyncio_default_fixture_loop_scope = "session" ``` 然后,我们在 `tests` 目录下新建 `conftest.py` 文件,添加以下内容: 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 01ff1664e884..f68aa5566deb 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 @@ -81,6 +81,7 @@ pip install pytest-asyncio ```toml [tool.pytest.ini_options] asyncio_mode = "auto" +asyncio_default_fixture_loop_scope = "session" ``` 然后,我们在 `tests` 目录下新建 `conftest.py` 文件,添加以下内容: From 02eb40fe13116d5925938e5341bbbfec47aeb1a5 Mon Sep 17 00:00:00 2001 From: Ju4tCode <42488585+yanyongyu@users.noreply.github.com> Date: Thu, 31 Oct 2024 16:07:51 +0800 Subject: [PATCH 3/3] :memo: add after nonebot init --- website/docs/best-practice/testing/README.mdx | 15 +++++++++++++-- .../best-practice/testing/README.mdx | 15 +++++++++++++-- .../best-practice/testing/README.mdx | 15 +++++++++++++-- .../best-practice/testing/README.mdx | 15 +++++++++++++-- 4 files changed, 52 insertions(+), 8 deletions(-) diff --git a/website/docs/best-practice/testing/README.mdx b/website/docs/best-practice/testing/README.mdx index f68aa5566deb..bba3012dfb49 100644 --- a/website/docs/best-practice/testing/README.mdx +++ b/website/docs/best-practice/testing/README.mdx @@ -93,7 +93,7 @@ import nonebot from nonebot.adapters.console import Adapter as ConsoleAdapter @pytest.fixture(scope="session", autouse=True) -def load_bot(): +async def after_nonebot_init(after_nonebot_init: None): # 加载适配器 driver = nonebot.get_driver() driver.register_adapter(ConsoleAdapter) @@ -104,9 +104,10 @@ def load_bot(): 这样,我们就可以在测试中使用机器人的插件了。通常,我们不需要自行初始化 NoneBot,NoneBug 已经为我们运行了 `nonebot.init()`。如果需要自定义 NoneBot 初始化的参数,我们可以在 `conftest.py` 中添加 `pytest_configure` 钩子函数。例如,我们可以修改 NoneBot 配置环境为 `test` 并从环境变量中输入配置: -```python {3,5,7-9} title=tests/conftest.py +```python {4,6,8-10} title=tests/conftest.py import os +import pytest from nonebug import NONEBOT_INIT_KWARGS os.environ["ENVIRONMENT"] = "test" @@ -115,6 +116,16 @@ def pytest_configure(config: pytest.Config): config.stash[NONEBOT_INIT_KWARGS] = {"secret": os.getenv("INPUT_SECRET")} ``` +NoneBug 默认也会为我们管理 lifespan 的 startup 与 shutdown。如果不希望 NoneBug 管理 lifespan,你可以在 `pytest_configure` 里添加以下配置: + +```python +import pytest +from nonebug import NONEBOT_START_LIFESPAN + +def pytest_configure(config: pytest.Config): + config.stash[NONEBOT_START_LIFESPAN] = False +``` + ## 编写插件测试 在配置完成插件加载后,我们就可以在测试中使用插件了。NoneBug 通过 pytest fixture `app` 提供各种测试方法,我们可以在测试中使用它来测试插件。现在,我们创建一个测试脚本来测试[深入指南](../../appendices/session-control.mdx)中编写的天气插件。首先,我们先要导入我们需要的模块: 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 f68aa5566deb..bba3012dfb49 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 @@ -93,7 +93,7 @@ import nonebot from nonebot.adapters.console import Adapter as ConsoleAdapter @pytest.fixture(scope="session", autouse=True) -def load_bot(): +async def after_nonebot_init(after_nonebot_init: None): # 加载适配器 driver = nonebot.get_driver() driver.register_adapter(ConsoleAdapter) @@ -104,9 +104,10 @@ def load_bot(): 这样,我们就可以在测试中使用机器人的插件了。通常,我们不需要自行初始化 NoneBot,NoneBug 已经为我们运行了 `nonebot.init()`。如果需要自定义 NoneBot 初始化的参数,我们可以在 `conftest.py` 中添加 `pytest_configure` 钩子函数。例如,我们可以修改 NoneBot 配置环境为 `test` 并从环境变量中输入配置: -```python {3,5,7-9} title=tests/conftest.py +```python {4,6,8-10} title=tests/conftest.py import os +import pytest from nonebug import NONEBOT_INIT_KWARGS os.environ["ENVIRONMENT"] = "test" @@ -115,6 +116,16 @@ def pytest_configure(config: pytest.Config): config.stash[NONEBOT_INIT_KWARGS] = {"secret": os.getenv("INPUT_SECRET")} ``` +NoneBug 默认也会为我们管理 lifespan 的 startup 与 shutdown。如果不希望 NoneBug 管理 lifespan,你可以在 `pytest_configure` 里添加以下配置: + +```python +import pytest +from nonebug import NONEBOT_START_LIFESPAN + +def pytest_configure(config: pytest.Config): + config.stash[NONEBOT_START_LIFESPAN] = False +``` + ## 编写插件测试 在配置完成插件加载后,我们就可以在测试中使用插件了。NoneBug 通过 pytest fixture `app` 提供各种测试方法,我们可以在测试中使用它来测试插件。现在,我们创建一个测试脚本来测试[深入指南](../../appendices/session-control.mdx)中编写的天气插件。首先,我们先要导入我们需要的模块: 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 f68aa5566deb..bba3012dfb49 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 @@ -93,7 +93,7 @@ import nonebot from nonebot.adapters.console import Adapter as ConsoleAdapter @pytest.fixture(scope="session", autouse=True) -def load_bot(): +async def after_nonebot_init(after_nonebot_init: None): # 加载适配器 driver = nonebot.get_driver() driver.register_adapter(ConsoleAdapter) @@ -104,9 +104,10 @@ def load_bot(): 这样,我们就可以在测试中使用机器人的插件了。通常,我们不需要自行初始化 NoneBot,NoneBug 已经为我们运行了 `nonebot.init()`。如果需要自定义 NoneBot 初始化的参数,我们可以在 `conftest.py` 中添加 `pytest_configure` 钩子函数。例如,我们可以修改 NoneBot 配置环境为 `test` 并从环境变量中输入配置: -```python {3,5,7-9} title=tests/conftest.py +```python {4,6,8-10} title=tests/conftest.py import os +import pytest from nonebug import NONEBOT_INIT_KWARGS os.environ["ENVIRONMENT"] = "test" @@ -115,6 +116,16 @@ def pytest_configure(config: pytest.Config): config.stash[NONEBOT_INIT_KWARGS] = {"secret": os.getenv("INPUT_SECRET")} ``` +NoneBug 默认也会为我们管理 lifespan 的 startup 与 shutdown。如果不希望 NoneBug 管理 lifespan,你可以在 `pytest_configure` 里添加以下配置: + +```python +import pytest +from nonebug import NONEBOT_START_LIFESPAN + +def pytest_configure(config: pytest.Config): + config.stash[NONEBOT_START_LIFESPAN] = False +``` + ## 编写插件测试 在配置完成插件加载后,我们就可以在测试中使用插件了。NoneBug 通过 pytest fixture `app` 提供各种测试方法,我们可以在测试中使用它来测试插件。现在,我们创建一个测试脚本来测试[深入指南](../../appendices/session-control.mdx)中编写的天气插件。首先,我们先要导入我们需要的模块: 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 f68aa5566deb..bba3012dfb49 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 @@ -93,7 +93,7 @@ import nonebot from nonebot.adapters.console import Adapter as ConsoleAdapter @pytest.fixture(scope="session", autouse=True) -def load_bot(): +async def after_nonebot_init(after_nonebot_init: None): # 加载适配器 driver = nonebot.get_driver() driver.register_adapter(ConsoleAdapter) @@ -104,9 +104,10 @@ def load_bot(): 这样,我们就可以在测试中使用机器人的插件了。通常,我们不需要自行初始化 NoneBot,NoneBug 已经为我们运行了 `nonebot.init()`。如果需要自定义 NoneBot 初始化的参数,我们可以在 `conftest.py` 中添加 `pytest_configure` 钩子函数。例如,我们可以修改 NoneBot 配置环境为 `test` 并从环境变量中输入配置: -```python {3,5,7-9} title=tests/conftest.py +```python {4,6,8-10} title=tests/conftest.py import os +import pytest from nonebug import NONEBOT_INIT_KWARGS os.environ["ENVIRONMENT"] = "test" @@ -115,6 +116,16 @@ def pytest_configure(config: pytest.Config): config.stash[NONEBOT_INIT_KWARGS] = {"secret": os.getenv("INPUT_SECRET")} ``` +NoneBug 默认也会为我们管理 lifespan 的 startup 与 shutdown。如果不希望 NoneBug 管理 lifespan,你可以在 `pytest_configure` 里添加以下配置: + +```python +import pytest +from nonebug import NONEBOT_START_LIFESPAN + +def pytest_configure(config: pytest.Config): + config.stash[NONEBOT_START_LIFESPAN] = False +``` + ## 编写插件测试 在配置完成插件加载后,我们就可以在测试中使用插件了。NoneBug 通过 pytest fixture `app` 提供各种测试方法,我们可以在测试中使用它来测试插件。现在,我们创建一个测试脚本来测试[深入指南](../../appendices/session-control.mdx)中编写的天气插件。首先,我们先要导入我们需要的模块: