Skip to content

Commit

Permalink
📝 add after nonebot init
Browse files Browse the repository at this point in the history
  • Loading branch information
yanyongyu authored Oct 31, 2024
1 parent 00e60e9 commit 02eb40f
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 8 deletions.
15 changes: 13 additions & 2 deletions website/docs/best-practice/testing/README.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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"
Expand All @@ -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)中编写的天气插件。首先,我们先要导入我们需要的模块:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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"
Expand All @@ -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)中编写的天气插件。首先,我们先要导入我们需要的模块:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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"
Expand All @@ -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)中编写的天气插件。首先,我们先要导入我们需要的模块:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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"
Expand All @@ -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)中编写的天气插件。首先,我们先要导入我们需要的模块:
Expand Down

0 comments on commit 02eb40f

Please sign in to comment.