Skip to content

Commit

Permalink
🐛 fix pytest.asyncio marker
Browse files Browse the repository at this point in the history
  • Loading branch information
RF-Tar-Railt committed Nov 21, 2024
1 parent 60a922a commit 8916c01
Show file tree
Hide file tree
Showing 6 changed files with 307 additions and 281 deletions.
554 changes: 286 additions & 268 deletions pdm.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ authors = [
]
dependencies = [
"tarina<0.7,>=0.6.3",
"nepattern<1.0,>=0.7.4",
"arclet-alconna<2.0,>=1.8.33",
"nepattern<1.0,>=0.7.7",
"arclet-alconna<2.0,>=1.8.34",
"arclet-alconna-tools>=0.7.10",
"importlib-metadata>=4.13.0",
"nonebot2>=2.3.0",
Expand Down
16 changes: 15 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import os
from typing import Any

import pytest
import nonebot
from nonebug import NONEBOT_INIT_KWARGS
from pytest_asyncio import is_async_test

# 导入适配器
from nonebot.adapters.qq import Adapter as QQAdapter
Expand All @@ -23,8 +25,20 @@ def pytest_configure(config: pytest.Config):
os.environ["PLUGIN_ALCONNA_TESTENV"] = "1"


def pytest_collection_modifyitems(items: Any) -> None:
"""
Make all tests run on the same event loop.
See: https://pytest-asyncio.readthedocs.io/en/latest/how-to-guides/run_session_tests_in_same_loop.html
"""
pytest_asyncio_tests = (item for item in items if is_async_test(item))
session_scope_marker = pytest.mark.asyncio(loop_scope="session")
for async_test in pytest_asyncio_tests:
async_test.add_marker(session_scope_marker, append=False)


@pytest.fixture(scope="session", autouse=True)
def after_nonebot_init(after_nonebot_init: None):
async def after_nonebot_init(after_nonebot_init: None):
# 加载适配器
driver = nonebot.get_driver()
driver.register_adapter(QQAdapter)
Expand Down
6 changes: 4 additions & 2 deletions tests/test_context.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import pytest
from nonebug import App
from nonebot import get_adapter
from nonebot.adapters.satori import Adapter
from nonebot.adapters.satori.models import User
from nonebot.adapters.satori import Bot, Message
from arclet.alconna import Args, Alconna, CommandMeta
Expand All @@ -22,8 +24,8 @@ async def tt_h(userid: str, selfid: str, ctx: dict):
await test_cmd.send(f"ok\n{userid}")

async with app.test_matcher(test_cmd) as ctx:
# adapter = get_adapter(Adapter)
bot = ctx.create_bot(base=Bot, login=FAKE_SATORI_LOGIN, info=None)
adapter = get_adapter(Adapter)
bot = ctx.create_bot(base=Bot, adapter=adapter, login=FAKE_SATORI_LOGIN, info=None)
msg = Message("test $(event.get_user_id()) $(bot.self_id)")
event = fake_message_event_satori(message=msg, id=123, user=User(id="456", name="test"))
ctx.receive_event(bot, event)
Expand Down
4 changes: 0 additions & 4 deletions tests/test_target_scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,6 @@ async def test_bots(app: App):
ctx.should_call_api("post_messages", {"channel_id": "789", "msg_id": "", "event_id": None, "content": "test"})
await target3.send(UniMessage("test"))

await asyncio.sleep(0.05)


@pytest.mark.asyncio()
async def test_enable(app: App, mocker: MockerFixture):
Expand Down Expand Up @@ -120,5 +118,3 @@ async def test_enable(app: App, mocker: MockerFixture):
driver = get_driver()
driver._bot_connection_hook.clear()
driver._bot_disconnection_hook.clear()

await asyncio.sleep(0.05)
4 changes: 0 additions & 4 deletions tests/test_uniseg.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import asyncio

import pytest
from nonebug import App
from nonebot import get_adapter
Expand Down Expand Up @@ -196,7 +194,6 @@ async def test_uniseg_recv(app: App):
)
msg = await UniMessage.generate(event=event2, bot=bot)
assert msg[UniReply, 0].msg
await asyncio.sleep(0.05)


@pytest.mark.asyncio()
Expand Down Expand Up @@ -241,4 +238,3 @@ async def handle(msg: MsgId):
)
target = Target("456", adapter=adapter.get_name())
await target.send("hello!")
await asyncio.sleep(0.05)

0 comments on commit 8916c01

Please sign in to comment.