技术债:统一本地测试入口(pytest pythonpath 配置,免 PYTHONPATH 拼接) #182
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
背景
2026-09-21 技术债务盘点发现:本地跑后端测试必须手工拼
PYTHONPATH=. .venv/bin/pytest,没有「开箱即跑」的统一入口。README 的
pip install -e ".[dev]"+pytest教程与实际已有
.venv环境脱节;而.venv_test是 SCRM 采集调试专用精简环境(无 fastapi),若误用它跑 pytest 会 collect 失败(43 个模块 ImportError),极易误判为「测试坏了」。
CI 侧已有
.gitea/workflows/test.yml(ruff + pytest 强制),本工单只补本地体验。影响范围
backend/pytest.ini(加 pytest 内置pythonpath配置,消除 PYTHONPATH 手工拼接)cd backend && .venv/bin/pytest直接可用)根因判断
pytest 从 7.0 起支持
pythonpath配置项(pytest.ini[pytest]段),把项目根目录注入 sys.path,即可替代
PYTHONPATH=.环境变量。属标准解法,非 hack。处理动作
backend/pytest.ini增加pythonpath = .配置项cd backend && .venv/bin/pytest tests/test_test_db_guard.py tests/test_smoke.py -q(不带 PYTHONPATH)通过cd backend && .venv/bin/pytest tests --collect-only -q)验收标准 / 关闭条件
backend/pytest.ini含pythonpath = .PYTHONPATH直接运行.venv/bin/pytest tests/test_test_db_guard.py tests/test_smoke.py11 个用例全部通过PYTHONPATH全量--collect-only无 ERROR(112 个测试文件全部收集成功)关联
关闭凭证核验 (2026-09-21)
backend/pytest.ini含pythonpath = .f551a9c,git show f551a9c -- backend/pytest.ini可见pythonpath = .配置项PYTHONPATH直接运行最小测试 11 个用例全部通过cd backend && .venv/bin/pytest tests/test_test_db_guard.py tests/test_smoke.py(无 PYTHONPATH 环境变量)→11 passed, 1 warning in 2.59sPYTHONPATH全量 collect 无 ERROR(112 个测试文件全部收集成功)cd backend && .venv/bin/pytest tests --collect-only -p no:warnings→1759 tests collected in 1.87s,无 ERROR提交:
f551a9c4e6结论: 关闭条件全部满足,关闭本单。