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

153 lines
4.5 KiB
Markdown
Raw 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.

# 风险检测系统单元测试
本目录包含风险检测系统的所有单元测试。
## 测试结构
```
tests/
├── conftest.py # pytest配置和共享fixtures
├── services/
│ └── risk_detection/
│ └── engine/ # 风险检测引擎测试
│ ├── test_dependency_resolver.py # 依赖解析器测试
│ ├── test_execution_plan.py # 执行计划器测试
│ ├── test_result_processor.py # 结果处理器测试
│ └── test_rule_engine.py # 规则引擎测试
└── README.md # 本文件
```
## 测试范围
### 1. 依赖解析器测试 (test_dependency_resolver.py)
- DependencyNode依赖节点的基本功能
- DependencyGraph依赖图构建、循环检测、层级计算
- DependencyResolver依赖分析、验证、执行顺序生成
### 2. 执行计划器测试 (test_execution_plan.py)
- ExecutionNode执行节点转换
- ExecutionStage执行阶段管理
- ExecutionPlan执行计划构建
- ExecutionPlanner计划生成串行/并行/混合模式)
- ExecutionPlanBuilder手动计划构建
### 3. 结果处理器测试 (test_result_processor.py)
- DetectionResult检测结果管理
- RiskEvidence风险证据管理
- ResultAggregator结果聚合
- RiskScoreCalculator风险评分计算
- EvidenceBuilder证据链构建
- SuggestionGenerator整改建议生成
- ResultProcessor结果处理流程
### 4. 规则引擎测试 (test_rule_engine.py)
- AlgorithmRegistry算法注册表
- RuleEngine规则引擎核心功能
- 全局规则引擎实例管理
## 运行测试
### 运行所有测试
```bash
# 在项目根目录执行
cd backend
python -m pytest tests/ -v
```
### 运行特定模块测试
```bash
# 运行依赖解析器测试
python -m pytest tests/services/risk_detection/engine/test_dependency_resolver.py -v
# 运行执行计划器测试
python -m pytest tests/services/risk_detection/engine/test_execution_plan.py -v
# 运行结果处理器测试
python -m pytest tests/services/risk_detection/engine/test_result_processor.py -v
# 运行规则引擎测试
python -m pytest tests/services/risk_detection/engine/test_rule_engine.py -v
```
### 运行特定测试用例
```bash
# 运行特定测试类
python -m pytest tests/services/risk_detection/engine/test_dependency_resolver.py::TestDependencyGraph -v
# 运行特定测试方法
python -m pytest tests/services/risk_detection/engine/test_dependency_resolver.py::TestDependencyGraph::test_has_cycle_with_cycle -v
```
### 生成覆盖率报告
```bash
# 安装coverage
pip install coverage
# 运行测试并生成覆盖率报告
coverage run -m pytest tests/
# 查看覆盖率报告
coverage report
# 生成HTML覆盖率报告
coverage html
```
## 测试数据
测试使用了以下模拟数据:
- 模拟数据库会话 (mock_db_session)
- 模拟检测规则列表 (sample_detection_rules)
- 模拟检测结果 (mock_detection_results)
- 模拟实体信息 (sample_entity_info)
- 模拟充值数据 (sample_recharge_data)
- 模拟申报数据 (sample_declaration_data)
- 模拟合同数据 (sample_contract_data)
- 模拟风险阈值 (sample_risk_thresholds)
## 测试工具和库
测试使用的主要工具:
- **pytest**:测试框架
- **pytest-asyncio**:异步测试支持
- **unittest.mock**:模拟对象和函数
- **coverage**:代码覆盖率测试
## 测试原则
1. **独立性**:每个测试用例独立运行,不依赖其他测试
2. **可重复性**:测试结果稳定,多次运行结果一致
3. **可读性**:测试代码清晰,注释完整
4. **覆盖率**核心逻辑覆盖率≥80%
5. **边界测试**:包含正常、异常、边界值测试
## 注意事项
1. 测试使用模拟对象,不依赖真实数据库
2. 异步测试使用pytest.mark.asyncio标记
3. 所有测试用例都有断言验证结果
4. 测试文件名规范test_<模块名>.py
5. 测试类名规范Test<组件名>
6. 测试方法名规范test_<功能描述>
## 添加新测试
添加新测试时,请遵循以下步骤:
1. 在相应模块的测试文件中添加测试类
2. 使用conftest.py中提供的fixtures
3. 添加适当的模拟数据和断言
4. 运行测试确保通过
5. 检查代码覆盖率
示例:
```python
class TestNewComponent:
def test_new_functionality(self):
"""测试新功能"""
# 准备测试数据
# 执行测试
# 断言结果
assert True
```