"""测试基座:httpx ASGITransport 进程内测试,不起真实服务。 需要本地 MySQL;测试库 app_test,与开发库隔离。 """ import os import pytest_asyncio from httpx import ASGITransport, AsyncClient os.environ.setdefault( "MYSQL_DSN", "mysql+aiomysql://root:root@127.0.0.1:3306/app_test?charset=utf8mb4" ) from src.main import create_app @pytest_asyncio.fixture async def client(): app = create_app() async with AsyncClient( transport=ASGITransport(app=app), base_url="http://test" ) as ac: # 手动触发 lifespan(ASGITransport 不自动跑 lifespan) async with app.router.lifespan_context(app): yield ac