deep-risk/backend/tests/TEST_SUMMARY.md
2025-12-14 20:08:27 +08:00

308 lines
6.7 KiB
Markdown
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.

# 风控检测系统测试文档
## 概述
本文档详细说明了风控检测系统的测试用例、测试方法和使用说明。测试覆盖了任务创建、查看报告等核心功能。
## 测试文件结构
```
/backend/app/tests/
├── conftest.py # 测试配置和共享fixtures
├── test_revenue_integrity.py # 收入完整性算法测试
├── test_task_manager.py # 任务管理器测试
├── test_detection_report.py # 检测报告查看测试
└── test_risk_detection_api.py # API端点测试
```
## 测试范围
### 1. 任务管理器测试 (`test_task_manager.py`)
**测试类: `TestTaskManager`**
覆盖场景:
- ✅ 创建按需检测任务ON_DEMAND
- ✅ 创建定期检测任务PERIODIC
- ✅ 创建批量检测任务BATCH
- ✅ 查询任务列表(支持状态、类型过滤)
- ✅ 获取任务详情
- ✅ 执行检测任务
- ✅ 任务不存在错误处理
- ✅ 任务执行失败处理
**测试类: `TestDetectionScheduler`**
覆盖场景:
- ✅ 调度定期检测任务
- ✅ 调度按需检测任务
- ✅ 调度批量检测任务
- ✅ 检查待执行任务
- ✅ 重试失败任务
- ✅ 清理旧任务
### 2. 检测报告查看测试 (`test_detection_report.py`)
**测试类: `TestDetectionReport`**
覆盖场景:
- ✅ 按任务ID查询检测结果
- ✅ 按实体ID查询检测结果
- ✅ 按风险等级查询检测结果
- ✅ 多条件组合查询
- ✅ 获取检测结果详情
- ✅ 获取检测结果汇总
- ✅ 空结果处理
- ✅ 风险分布统计
- ✅ 证据链结构验证
- ✅ 风险数据结构验证
### 3. API端点测试 (`test_risk_detection_api.py`)
**测试类: `TestRiskDetectionAPI`**
覆盖场景:
- ✅ 创建任务API
- ✅ 执行任务API
- ✅ 查询任务列表API
- ✅ 获取任务详情API
- ✅ 即时检测API
- ✅ 查询检测结果API
- ✅ 获取结果详情API
- ✅ 获取检测汇总API
- ✅ 获取算法列表API
- ✅ 获取规则列表API
- ✅ 参数验证错误处理
- ✅ 资源不存在错误处理
## 核心功能测试
### 1. 任务创建流程
**测试步骤:**
1. 创建按需检测任务
2. 验证任务创建成功
- 任务ID生成
- 任务状态为PENDING
- 实体数量正确
- 参数保存正确
3. 执行检测任务
4. 验证执行结果
- 任务状态更新为COMPLETED
- 执行进度更新
- 结果统计正确
### 2. 报告查看流程
**测试步骤:**
1. 查询检测结果(支持多条件过滤)
2. 获取结果详情
3. 获取汇总报告
## 运行测试
### 1. 安装依赖
```bash
cd /backend
source venv/bin/activate
pip install -r requirements.txt
```
### 2. 运行所有测试
```bash
# 运行所有测试
pytest app/tests/ -v
# 运行特定测试文件
pytest app/tests/test_task_manager.py -v
pytest app/tests/test_detection_report.py -v
pytest app/tests/test_risk_detection_api.py -v
# 运行特定测试类
pytest app/tests/test_task_manager.py::TestTaskManager -v
# 运行特定测试方法
pytest app/tests/test_task_manager.py::TestTaskManager::test_create_task_on_demand -v
```
### 3. 生成覆盖率报告
```bash
# 安装coverage
pip install coverage
# 运行测试并生成覆盖率报告
coverage run -m pytest app/tests/
coverage report
# 生成HTML覆盖率报告
coverage html
```
## 主要测试用例
### 任务管理器测试用例
```python
# 创建按需检测任务
test_create_task_on_demand()
# 创建定期检测任务
test_create_task_periodic()
# 创建批量检测任务
test_create_task_batch()
# 查询任务列表
test_list_tasks()
# 执行检测任务
test_execute_task_success()
# 任务不存在处理
test_execute_task_not_found()
# 任务执行失败处理
test_execute_task_with_failure()
```
### 检测报告测试用例
```python
# 条件过滤查询
test_list_results_with_task_filter()
test_list_results_with_entity_filter()
test_list_results_with_risk_level_filter()
# 结果详情
test_get_result_detail()
test_get_result_not_found()
# 汇总报告
test_get_detection_summary()
test_get_detection_summary_no_results()
test_get_detection_summary_critical_risk()
# 数据结构验证
test_result_evidence_structure()
test_result_risk_data_structure()
```
### API端点测试用例
```python
# 任务管理
POST /api/v1/risk-detection/tasks
POST /api/v1/risk-detection/tasks/{task_id}/execute
GET /api/v1/risk-detection/tasks
GET /api/v1/risk-detection/tasks/{task_id}
# 即时检测
POST /api/v1/risk-detection/execute
# 结果查询
GET /api/v1/risk-detection/results
GET /api/v1/risk-detection/results/{result_id}
# 汇总报告
GET /api/v1/risk-detection/summary
# 算法和规则
GET /api/v1/risk-detection/algorithms
GET /api/v1/risk-detection/rules
```
## 错误处理测试
### 场景1任务不存在
```python
with pytest.raises(ValueError, match="任务不存在"):
await task_manager.execute_task("task_not_exist")
```
### 场景2结果不存在
```python
with pytest.raises(HTTPException) as exc_info:
await get_result(result_id=9999)
assert exc_info.value.status_code == 404
```
### 场景3参数验证错误
```python
response = client.post("/api/v1/risk-detection/tasks", json={})
assert response.status_code == 422
```
## 性能测试
### 测试指标
1. **任务创建速度**
- 单实体:< 100ms
- 批量实体100个< 1s
- 批量实体1000个< 5s
2. **任务执行速度**
- 简单算法< 500ms/实体
- 复杂算法< 2s/实体
3. **查询响应时间**
- 任务列表< 200ms
- 结果查询< 300ms
- 汇总报告< 500ms
## 最佳实践
### 1. 测试命名规范
- 测试文件名`test_<module>.py`
- 测试类名:`Test<ClassName>`
- 测试方法名:`test_<scenario>_<expected_behavior>`
### 2. 测试数据管理
- 使用fixtures共享测试数据
- 确保测试数据独立性
- 及时清理测试数据
### 3. Mock使用指南
- 只Mock外部依赖
- 保持Mock逻辑简单
- 验证Mock调用
### 4. 断言建议
- 使用具体断言而非通用断言
- 验证关键字段而非全部字段
- 提供清晰的错误消息
## 总结
本测试套件全面覆盖了风控检测系统的核心功能:
1. **任务管理**:创建、查询、执行、调度
2. **报告查看**:结果查询、详情获取、汇总分析
3. **API端点**完整的RESTful API测试
4. **错误处理**:各种异常场景测试
5. **性能测试**:响应时间和并发能力
通过这些测试用例,可以确保:
- 功能正确性
- 数据一致性
- 错误处理健壮性
- API接口稳定性
- 性能达标
建议定期运行测试,特别是:
- 代码提交前
- 发布前
- 性能调优后
- 依赖更新后
---
**更新日期**: 2024-11-30
**版本**: v1.0
**维护者**: 风控检测开发团队