测试指南
测试目录结构
tests/
├── conftest.py # 全局 fixtures 和配置
├── unit/ # 单元测试(无外部依赖,可离线运行)
│ ├── test_paste.py # 基础导入和版本
│ ├── test_snow_id.py # 雪花ID生成器
│ ├── test_jwt.py # JWT 令牌(mock)
│ ├── test_configure.py # 配置管理(mock)
│ ├── test_pagination.py # 分页逻辑
│ ├── test_ustr.py # 字符串工具
│ └── test_udict.py # 字典工具
├── integration/ # 集成测试(需要真实服务)
│ └── test_db.py # 数据库连接测试
└── README.md # 本文件
运行方式
运行所有单元测试(推荐日常使用)
cd <项目根目录>
pytest tests/unit/ -v
运行所有测试(含集成测试)
pytest tests/ -v
仅运行集成测试
pytest tests/integration/ -v
生成覆盖率报告
pytest tests/unit/ --cov=paste --cov-report=html
open htmlcov/index.html
编写规范
- 单元测试放在
tests/unit/,无外部依赖 - 集成测试放在
tests/integration/,加@pytest.mark.integration - 每个测试类以
Test开头,测试方法以test_开头 - 使用断言而非 print 验证结果
- 不依赖外部配置文件或数据库连接