nonebot2/website/versioned_docs/version-2.0.0-beta.3/advanced/unittest/test-matcher.md
github-actions[bot] 9b45b77894 🔖 Release 2.0.0-beta.3
2022-05-20 10:21:32 +00:00

4.7 KiB
Raw Blame History

sidebar_position description
2 测试事件响应和 API 调用

测试事件响应和 API 调用

事件响应器通过 RulePermission 来判断当前事件是否触发事件响应器,通过 send 发送消息或使用 call_api 调用平台 API这里我们将对上述行为进行测试。

定义预期响应行为

NoneBug 提供了六种定义 RulePermission 的预期行为的方法来进行测试:

  • should_pass_rule
  • should_not_pass_rule
  • should_ignore_rule
  • should_pass_permission
  • should_not_pass_permission
  • should_ignore_permission

以下为示例代码

示例插件
from nonebot import on_message

async def always_pass():
    return True

async def never_pass():
    return False

foo = on_message(always_pass)
bar = on_message(never_pass, permission=never_pass)
import pytest
from nonebug import App

@pytest.mark.asyncio
async def test_matcher(app: App, load_plugins):
    from awesome_bot.plugins.example import foo, bar

    async with app.test_matcher(foo) as ctx:
        bot = ctx.create_bot()
        event = make_fake_event()()  # 此处替换为平台事件
        ctx.receive_event(bot, event)
        ctx.should_pass_rule()
        ctx.should_pass_permission()

    async with app.test_matcher(bar) as ctx:
        bot = ctx.create_bot()
        event = make_fake_event()()  # 此处替换为平台事件
        ctx.receive_event(bot, event)
        ctx.should_not_pass_rule()
        ctx.should_not_pass_permission()

    # 如需忽略规则/权限不通过
    async with app.test_matcher(bar) as ctx:
        bot = ctx.create_bot()
        event = make_fake_event()()  # 此处替换为平台事件
        ctx.receive_event(bot, event)
        ctx.should_ignore_rule()
        ctx.should_ignore_permission()

定义预期 API 调用行为

事件响应器操作调用平台 API 中,我们已经了解如何向发送消息或调用平台 API。接下来对 sendcall_api 进行测试。

should_call_send

定义事件响应器预期发送消息,包括使用 sendfinishpausereject 以及 got 的 prompt 等方法发送的消息。

should_call_send 需要提供四个参数:

  • event:事件对象。
  • message:预期的消息对象,可以是strMessageMessageSegment
  • resultsend 的返回值,将会返回给插件。
  • **kwargssend 方法的额外参数。
示例插件
from nonebot import on_message

foo = on_message()

@foo.handle()
async def _():
    await foo.send("test")
import pytest
from nonebug import App

@pytest.mark.asyncio
async def test_matcher(app: App, load_plugins):
    from awesome_bot.plugins.example import foo

    async with app.test_matcher(foo) as ctx:
        bot = ctx.create_bot()
        event = make_fake_event()()  # 此处替换为平台事件
        ctx.receive_event(bot, event)
        ctx.should_call_send(event, "test", True)

should_call_api

定义事件响应器预期调用机器人 API 接口,包括使用 call_api 或者直接使用 bot.some_api 的方式调用 API。

should_call_api 需要提供四个参数:

  • apiAPI 名称。
  • data:预期的请求数据。
  • resultcall_api 的返回值,将会返回给插件。
  • **kwargscall_api 方法的额外参数。
示例插件
from nonebot import on_message
from nonebot.adapters import Bot

foo = on_message()


@foo.handle()
async def _(bot: Bot):
  await bot.example_api(test="test")
import pytest
from nonebug import App

@pytest.mark.asyncio
async def test_matcher(app: App, load_plugins):
    from awesome_bot.plugins.example import foo

    async with app.test_matcher(foo) as ctx:
        bot = ctx.create_bot()
        event = make_fake_event()()  # 此处替换为平台事件
        ctx.receive_event(bot, event)
        ctx.should_call_api("example_api", {"test": "test"}, True)