- tests/unit/modules/test_repository.py:base_repository 在 SQLite 内存库的 CRUD 测试 - tests/integration/api/test_smoke.py:登录链路 / 只读接口 / 未授权拦截(标记 integration,默认跳过) - conftest 提供 sqlite_db(隔离 DB)与 app/client/admin_headers(集成)fixture - pytest.ini 默认跳过 integration,Makefile test-integration 可显式运行 - 修复 base_repository.get_by_id/get_by_ids 对字符串 ID 的兼容(URL 路径参数) - 示范 modules/system/level service 改用 level_repo,CRUD 全链路实测通过 Co-Authored-By: Claude <noreply@anthropic.com>
33 lines
921 B
Python
33 lines
921 B
Python
"""集成测试 fixture:真实 FastAPI 应用 + ASGI client(需 MySQL/Redis 运行)。"""
|
|
|
|
import httpx
|
|
import pytest
|
|
import pytest_asyncio
|
|
|
|
from core import jwt as core_jwt
|
|
from core.app import create_app
|
|
|
|
|
|
@pytest.fixture
|
|
def app():
|
|
return create_app()
|
|
|
|
|
|
@pytest_asyncio.fixture
|
|
async def client(app):
|
|
# 手动运行 Redis lifespan(当前 httpx 版本不支持 ASGITransport lifespan 参数)
|
|
from core.redis_client import redis_lifespan
|
|
|
|
transport = httpx.ASGITransport(app=app)
|
|
async with redis_lifespan(app):
|
|
async with httpx.AsyncClient(transport=transport, base_url="http://test") as c:
|
|
yield c
|
|
|
|
|
|
@pytest.fixture
|
|
def admin_headers():
|
|
"""userId=1 管理员 token(绕过 permission_required 权限校验)。"""
|
|
token = core_jwt.create_token(
|
|
{"userId": 1, "username": "admin", "realname": "admin"})
|
|
return {"Authorization": f"Bearer {token}"}
|