🐛 测试修复

This commit is contained in:
HibiKier 2025-07-03 17:35:43 +08:00
parent 10009f6827
commit c1626bbbfe
2 changed files with 12 additions and 2 deletions

View File

@ -116,6 +116,7 @@ async def app(app: App, tmp_path: Path, mocker: MockerFixture):
await init()
# await driver._lifespan.startup()
os.environ["AIOCACHE_DISABLE"] = "1"
os.environ["PYTEST_CURRENT_TEST"] = "1"
yield app

View File

@ -1,6 +1,7 @@
import asyncio
from collections.abc import AsyncGenerator, Awaitable, Callable, Sequence
from contextlib import asynccontextmanager
import os
from pathlib import Path
import time
from typing import Any, ClassVar, cast
@ -33,7 +34,7 @@ driver = nonebot.get_driver()
_client: AsyncClient | None = None
@PriorityLifecycle.on_startup(priority=5)
@PriorityLifecycle.on_startup(priority=0)
async def _():
"""
在Bot启动时初始化全局httpx客户端
@ -83,8 +84,16 @@ def get_client() -> AsyncClient:
"""
获取全局 httpx.AsyncClient 实例
"""
global _client
if not _client:
raise RuntimeError("全局 httpx.AsyncClient 未初始化,请检查启动流程。")
if not os.environ.get("PYTEST_CURRENT_TEST"):
raise RuntimeError("全局 httpx.AsyncClient 未初始化,请检查启动流程。")
# 在测试环境中创建临时客户端
logger.warning("在测试环境中创建临时HTTP客户端", "HTTPClient")
_client = httpx.AsyncClient(
headers=get_user_agent(),
follow_redirects=True,
)
return _client