PayrollManagementSystem/backend/tests/conftest.py
liuwenxin 273ebf4657 chore: 初始化项目结构与登录鉴权模块
后端 FastAPI 六层结构(api/services/repositories/models/schemas/core),
统一响应信封、全局异常、日志脱敏;6 张表经 Alembic 迁移建库,SQL 留存
db/migration;登录鉴权含图形验证码(Redis GETDEL 防重放)、JWT、登录限流、
登出黑名单(TTL=令牌剩余有效期)、登录日志。

前端 Vue3 + TS + Element Plus 骨架,含登录页、路由守卫、请求封装与 Vite 代理。

工程配置:uv 锁定 Python 3.12、ruff 检查与格式化、editorconfig、gitattributes。

测试 184 项(单元 155 / 集成 29)全绿,浏览器端到端 8 项通过。

Co-Authored-By: Claude Code <noreply@anthropic.com>
2026-10-09 18:46:36 +08:00

39 lines
1.2 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""测试共用夹具。"""
import pytest
from fastapi.testclient import TestClient
@pytest.fixture(scope="session")
def client() -> TestClient:
"""构造应用测试客户端(会话级复用,避免重复构建应用)。
注意:测试环境未必有 MySQL/Redis 在跑,健康检查会如实返回 false,
因此断言应聚焦于响应结构而非依赖的连通性。
"""
from app.main import app
with TestClient(app) as test_client:
yield test_client
@pytest.fixture
def fake_redis(monkeypatch: pytest.MonkeyPatch):
"""把 Redis 客户端替换为进程内假实现。
验证码的 GETDEL、限流的 INCR/EXPIRE/TTL、黑名单的 SETEX——都只用到
基础字符串命令,fakeredis 能完整覆盖,因此这些逻辑无需起容器即可测试,
单测得以保持秒级。
替换的是 `app.core.redis_client` 的模块级单例,各 service 通过
`get_redis()` 取用,故一处替换即全局生效。
"""
import fakeredis
from app.core import redis_client
fake = fakeredis.FakeRedis(decode_responses=True)
monkeypatch.setattr(redis_client, "_client", fake)
yield fake
fake.close()