添加单元测试 (#513)

*  添加测试依赖 nonebug

*  添加测试依赖 pytest-asyncio

* 🔧 配置 pytest

* 🙈 更新 .gitignore

*  添加 test 依赖 pytest-cov

* 🔧 配置 pytest

*  添加测试

* 👷 添加 Test CI

* 💚 暂时移除 3.13 的测试
This commit is contained in:
呵呵です
2024-11-01 01:33:18 +08:00
committed by GitHub
parent 7555297e1e
commit dfb19f150a
8 changed files with 925 additions and 23 deletions

0
tests/__init__.py Normal file
View File

21
tests/conftest.py Normal file
View File

@@ -0,0 +1,21 @@
import os
import nonebot
import pytest
from nonebot.adapters.onebot.v11 import Adapter as OnebotV11Adapter
from nonebug import NONEBOT_INIT_KWARGS, NONEBOT_START_LIFESPAN # type: ignore[import-untyped]
os.environ['ENVIRONMENT'] = 'test'
def pytest_configure(config: pytest.Config) -> None:
config.stash[NONEBOT_INIT_KWARGS] = {'log_level': 'DEBUG'}
config.stash[NONEBOT_START_LIFESPAN] = False
@pytest.fixture(scope='session', autouse=True)
async def after_nonebot_init(after_nonebot_init: None) -> None: # noqa: ARG001
driver = nonebot.get_driver()
driver.register_adapter(OnebotV11Adapter)
nonebot.load_from_toml('pyproject.toml')

28
tests/fake_event.py Normal file
View File

@@ -0,0 +1,28 @@
from datetime import datetime
from typing import Literal
from zoneinfo import ZoneInfo
from nonebot.adapters.onebot.v11 import GroupMessageEvent
from nonebot.adapters.onebot.v11.event import Sender
from pydantic import Field
class FakeGroupMessageEvent(GroupMessageEvent):
time: int = Field(default_factory=lambda: int(datetime.now(tz=ZoneInfo('Asia/Shanghai')).timestamp()))
self_id: int = 1
post_type: Literal['message'] = 'message'
sub_type: str = 'normal'
user_id: int = 10
message_type: Literal['group'] = 'group'
group_id: int = 10000
message_id: int = 1
font: int = 0
sender: Sender = Sender(
card='',
nickname='test',
role='member',
)
to_me: bool = False
class Config:
extra = 'allow'

19
tests/tetrio/test_bind.py Normal file
View File

@@ -0,0 +1,19 @@
import pytest
from nonebot.adapters.onebot.v11 import Message
from nonebug import App # type: ignore[import-untyped]
from tests.fake_event import FakeGroupMessageEvent
@pytest.mark.asyncio
async def test_invalid_name(app: App) -> None:
from nonebot_plugin_tetris_stats.games import alc
raw_message = 'tstats tetrio bind 芜湖'
message = Message(raw_message)
event = FakeGroupMessageEvent(message=message, original_message=message, raw_message=raw_message)
async with app.test_matcher(alc) as ctx:
bot = ctx.create_bot()
ctx.receive_event(bot, event)
ctx.should_finished(alc)
ctx.should_call_send(event, '用户名/ID不合法', result=None)