You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
97 lines
1.9 KiB
97 lines
1.9 KiB
# 后端测试套件
|
|
|
|
## 测试文件说明
|
|
|
|
- `test_api.py` - API 端点集成测试
|
|
- `test_services.py` - 服务层单元测试
|
|
|
|
## 运行测试
|
|
|
|
### 运行所有测试
|
|
```bash
|
|
cd backend
|
|
pytest
|
|
```
|
|
|
|
### 运行特定测试文件
|
|
```bash
|
|
pytest tests/test_services.py -v
|
|
```
|
|
|
|
### 运行特定测试类
|
|
```bash
|
|
pytest tests/test_services.py::TestAuthService -v
|
|
```
|
|
|
|
### 运行特定测试函数
|
|
```bash
|
|
pytest tests/test_services.py::TestAuthService::test_password_hashing -v
|
|
```
|
|
|
|
### 生成覆盖率报告
|
|
```bash
|
|
pytest --cov=app --cov-report=html
|
|
# 报告生成在 htmlcov/index.html
|
|
```
|
|
|
|
### 运行测试并显示输出
|
|
```bash
|
|
pytest -s -v
|
|
```
|
|
|
|
## 测试覆盖
|
|
|
|
### 认证服务 (auth_service.py)
|
|
- ✅ 密码哈希
|
|
- ✅ 密码验证
|
|
- ✅ 访问令牌创建
|
|
- ✅ 刷新令牌创建
|
|
- ✅ 令牌解码
|
|
- ✅ API Key 生成
|
|
|
|
### K 线数据服务 (kline_service.py)
|
|
- ✅ 获取 K 线数据
|
|
- ✅ 获取最新 K 线
|
|
- ✅ 获取品种列表
|
|
- ✅ 获取周期列表
|
|
|
|
### 告警服务 (alert_service.py)
|
|
- ✅ 创建告警
|
|
- ✅ 获取用户告警
|
|
- ✅ 更新告警
|
|
- ✅ 删除告警
|
|
|
|
### 订阅服务 (subscription_service.py)
|
|
- ✅ 创建订阅
|
|
- ✅ 获取用户订阅
|
|
- ✅ 取消订阅
|
|
|
|
### API 端点
|
|
- ✅ 健康检查
|
|
- ✅ 认证端点
|
|
- ✅ K 线数据端点
|
|
- ✅ 用户管理端点
|
|
- ✅ 告警管理端点
|
|
- ✅ 订阅管理端点
|
|
|
|
## Mock 说明
|
|
|
|
测试使用 `unittest.mock` 来模拟数据库连接和外部依赖:
|
|
|
|
```python
|
|
from unittest.mock import Mock, patch, MagicMock
|
|
|
|
@patch('app.services.kline_service.TimescaleSessionLocal')
|
|
def test_get_kline_data(self, mock_session):
|
|
mock_db = MagicMock()
|
|
mock_session.return_value.__enter__.return_value = mock_db
|
|
# ... 测试代码
|
|
```
|
|
|
|
## 注意事项
|
|
|
|
1. 测试数据库使用 SQLite 内存数据库,不影响生产数据
|
|
2. 所有测试都是独立的,可以单独运行
|
|
3. 测试数据在测试结束后自动清理
|
|
4. 运行测试前确保已安装测试依赖:`pip install -r requirements.txt`
|