chore: P1 工程化工具链
- pyproject.toml:pytest/black/isort/mypy 统一配置 - requirements-dev.txt:测试与代码规范开发依赖 - .flake8 / .editorconfig:PEP8 与编辑器统一 - .pre-commit-config.yaml:提交前钩子 - Makefile:常用命令集 - .env.example:环境变量模板(76 键) - tests/:pytest 骨架 + R 响应格式冒烟测试 Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude
parent
15f2a5e097
commit
8691412ec6
@@ -0,0 +1 @@
|
||||
"""测试包:pytest 通过此包将项目根目录加入 sys.path。"""
|
||||
@@ -0,0 +1,19 @@
|
||||
"""
|
||||
pytest 全局配置
|
||||
=============================================================
|
||||
|
||||
P1 阶段仅搭建骨架,保证 `pytest` 可空跑。
|
||||
测试 App 构建、数据库/Redis fixture 在 P8(测试完善阶段)补充:
|
||||
- app fixture:create_app() + 测试库 DATABASE_URL 覆盖
|
||||
- db fixture:事务内建表/清库
|
||||
- client fixture:Flask test_client
|
||||
"""
|
||||
|
||||
import os
|
||||
import sys
|
||||
|
||||
# 项目根目录加入 sys.path(tests/ 已有 __init__.py 时 pytest 通常自动处理,
|
||||
# 此处显式补充,兼容从任意目录启动 pytest 的场景)
|
||||
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
if ROOT not in sys.path:
|
||||
sys.path.insert(0, ROOT)
|
||||
@@ -0,0 +1 @@
|
||||
"""单元测试包。"""
|
||||
@@ -0,0 +1 @@
|
||||
"""core 层单元测试。"""
|
||||
@@ -0,0 +1,53 @@
|
||||
"""
|
||||
R 统一响应格式单元测试
|
||||
|
||||
验证 `R.ok()` / `R.failed()` 返回的 JSON 响应结构符合前后端约定:
|
||||
{code, data, msg, ok}(code=0 成功,非 0 失败)。
|
||||
"""
|
||||
|
||||
from utils import R
|
||||
|
||||
|
||||
class TestOk:
|
||||
"""成功响应"""
|
||||
|
||||
def test_ok_default_format(self):
|
||||
resp = R.ok()
|
||||
body = resp.get_json()
|
||||
|
||||
assert body["code"] == 0
|
||||
assert body["ok"] is True
|
||||
assert body["data"] is None
|
||||
assert body["msg"]
|
||||
|
||||
def test_ok_with_data(self):
|
||||
resp = R.ok(data={"id": 1, "name": "张三"})
|
||||
body = resp.get_json()
|
||||
|
||||
assert body["code"] == 0
|
||||
assert body["data"] == {"id": 1, "name": "张三"}
|
||||
|
||||
def test_ok_with_kwargs(self):
|
||||
resp = R.ok(data=[], total=100)
|
||||
body = resp.get_json()
|
||||
|
||||
assert body["total"] == 100
|
||||
|
||||
|
||||
class TestFailed:
|
||||
"""失败响应"""
|
||||
|
||||
def test_failed_default_format(self):
|
||||
resp = R.failed()
|
||||
body = resp.get_json()
|
||||
|
||||
assert body["code"] != 0
|
||||
assert body["ok"] is False
|
||||
assert body["data"] is None
|
||||
|
||||
def test_failed_custom_code(self):
|
||||
resp = R.failed(msg="未授权访问", code=401)
|
||||
body = resp.get_json()
|
||||
|
||||
assert body["code"] == 401
|
||||
assert body["msg"] == "未授权访问"
|
||||
Reference in New Issue
Block a user