nonebot / nonebug

NoneBot2 测试框架 / NoneBot2 test framework
https://nonebot.dev/docs/best-practice/testing/
MIT License
28 stars 6 forks source link

运行测试时要如何调用@driver.on_startup装饰的函数呢 #23

Closed Cypas closed 7 months ago

Cypas commented 7 months ago

插件内sql的连接初始化是写在@driver.on_startup装饰函数内的,运行nonebug测试时并不会自动调用

yanyongyu commented 7 months ago

因为nonebug并不会运行driver,所以你可以自行在pytest session start的时候调用,通过fixture的形式

Cypas commented 7 months ago

已解决

@pytest.fixture(scope="session", autouse=True)
def load_bot():
    # 加载适配器
    driver = nonebot.get_driver()
    driver.register_adapter(V11Adapter)
    # 加载插件
    nonebot.load_plugins("nonebot_plugin_splatoon3_nso")  # 本地插件
    # 手动运行适配器载入函数
    from nonebot_plugin_splatoon3_nso import bot_on_start
    asyncio.run(bot_on_start())