46 lines
1.4 KiB
Python
46 lines
1.4 KiB
Python
"""Pytest 共享 fixture:FastAPI 应用与 TestClient。"""
|
||
|
||
from __future__ import annotations
|
||
|
||
import asyncio
|
||
import inspect
|
||
|
||
import pytest
|
||
from fastapi.testclient import TestClient
|
||
|
||
try:
|
||
from app.main import app # type: ignore
|
||
except Exception: # noqa: BLE001
|
||
# 测试环境里有些可选依赖(例如 langgraph)可能未安装。
|
||
# 不要让整个测试套件在导入 conftest 时直接失败;仅在需要 client 的测试里跳过。
|
||
app = None
|
||
|
||
|
||
@pytest.fixture
|
||
def client() -> TestClient:
|
||
"""FastAPI 应用 TestClient,用于集成测试。"""
|
||
if app is None:
|
||
pytest.skip("FastAPI app 依赖未满足(例如缺少 langgraph),跳过需要 client 的集成测试。")
|
||
return TestClient(app)
|
||
|
||
|
||
def pytest_configure(config: pytest.Config) -> None:
|
||
"""为轻量测试环境补齐 asyncio marker。"""
|
||
config.addinivalue_line("markers", "asyncio: mark test as asyncio coroutine")
|
||
|
||
|
||
@pytest.hookimpl(tryfirst=True)
|
||
def pytest_pyfunc_call(pyfuncitem: pytest.Function) -> bool | None:
|
||
"""在未安装 pytest-asyncio 的环境中兜底执行 async 测试。"""
|
||
|
||
if not inspect.iscoroutinefunction(pyfuncitem.obj):
|
||
return None
|
||
|
||
funcargs = {
|
||
arg: pyfuncitem.funcargs[arg]
|
||
for arg in pyfuncitem._fixtureinfo.argnames
|
||
if arg in pyfuncitem.funcargs
|
||
}
|
||
asyncio.run(pyfuncitem.obj(**funcargs))
|
||
return True
|