# 语义逻辑审查模块单元测试 ## 📋 测试概述 本测试套件用于测试 `core/construction_review/component/reviewers/semantic_logic.py` 模块的功能。 ## 🎯 测试覆盖范围 ### 1. 基础功能测试 (TestSemanticLogicReviewer) - ✅ 审查器初始化测试 - ✅ 全局单例实例测试 - ✅ 模型配置验证 - ✅ 语义逻辑检查成功场景 - ✅ 无状态字典的检查场景 - ✅ API 调用失败处理 - ✅ 空内容处理 - ✅ 带参考信息的检查 - ✅ 消息格式转换 - ✅ 执行时间跟踪 ### 2. 集成测试 (TestIntegration) - ✅ 完整工作流程测试(需要实际 API) ### 3. 边界情况测试 (TestEdgeCases) - ✅ 超长内容处理 - ✅ 特殊字符处理 - ✅ Unicode 字符处理 ## 🚀 运行测试 ### 前置条件 确保已安装必要的依赖: ```bash pip install pytest pytest-asyncio pytest-mock ``` ### 运行所有测试 ```bash # 在项目根目录下运行 cd /h:/UGit/LQAgentPlatform # 运行所有测试 pytest Semantic_Logic_Test/test_semantic_logic.py -v # 运行测试并显示详细输出 pytest Semantic_Logic_Test/test_semantic_logic.py -v -s # 运行测试并生成覆盖率报告 pytest Semantic_Logic_Test/test_semantic_logic.py --cov=core.construction_review.component.reviewers.semantic_logic --cov-report=html ``` ### 运行特定测试类 ```bash # 只运行基础功能测试 pytest Semantic_Logic_Test/test_semantic_logic.py::TestSemanticLogicReviewer -v # 只运行边界情况测试 pytest Semantic_Logic_Test/test_semantic_logic.py::TestEdgeCases -v ``` ### 运行特定测试方法 ```bash # 测试初始化 pytest Semantic_Logic_Test/test_semantic_logic.py::TestSemanticLogicReviewer::test_reviewer_initialization -v # 测试成功场景 pytest Semantic_Logic_Test/test_semantic_logic.py::TestSemanticLogicReviewer::test_check_semantic_logic_success -v # 测试错误处理 pytest Semantic_Logic_Test/test_semantic_logic.py::TestSemanticLogicReviewer::test_check_semantic_logic_api_error -v ``` ### 跳过集成测试 集成测试需要实际的 API 服务可用,如果不想运行集成测试: ```bash pytest Semantic_Logic_Test/test_semantic_logic.py -v -m "not integration" ``` ## 📊 测试结果示例 ``` ============================= test session starts ============================= platform win32 -- Python 3.x.x, pytest-7.x.x, pluggy-1.x.x collected 15 items test_semantic_logic.py::TestSemanticLogicReviewer::test_reviewer_initialization PASSED [ 6%] test_semantic_logic.py::TestSemanticLogicReviewer::test_global_singleton_instance PASSED [ 13%] test_semantic_logic.py::TestSemanticLogicReviewer::test_model_config PASSED [ 20%] test_semantic_logic.py::TestSemanticLogicReviewer::test_check_semantic_logic_success PASSED [ 26%] test_semantic_logic.py::TestSemanticLogicReviewer::test_check_semantic_logic_without_state PASSED [ 33%] test_semantic_logic.py::TestSemanticLogicReviewer::test_check_semantic_logic_api_error PASSED [ 40%] test_semantic_logic.py::TestSemanticLogicReviewer::test_check_semantic_logic_empty_content PASSED [ 46%] test_semantic_logic.py::TestSemanticLogicReviewer::test_check_semantic_logic_with_references PASSED [ 53%] test_semantic_logic.py::TestSemanticLogicReviewer::test_message_format_conversion PASSED [ 60%] test_semantic_logic.py::TestSemanticLogicReviewer::test_execution_time_tracking PASSED [ 66%] test_semantic_logic.py::TestIntegration::test_full_workflow SKIPPED [ 73%] test_semantic_logic.py::TestEdgeCases::test_very_long_content PASSED [ 80%] test_semantic_logic.py::TestEdgeCases::test_special_characters PASSED [ 86%] test_semantic_logic.py::TestEdgeCases::test_unicode_content PASSED [ 93%] ======================== 14 passed, 1 skipped in 2.34s ======================== ``` ## 🔍 测试详解 ### 1. 初始化测试 验证 `SemanticLogicReviewer` 类是否正确初始化,包括: - OpenAI 客户端创建 - 模型配置加载 - 参数设置 ### 2. 成功场景测试 模拟正常的语义逻辑检查流程: - 提示词模板加载 - OpenAI API 调用 - 结果处理 - 进度通知 ### 3. 错误处理测试 验证各种错误场景的处理: - API 连接失败 - 超时处理 - 异常捕获 - 错误通知 ### 4. 边界情况测试 测试极端情况下的系统行为: - 超长文本(10000+ 字符) - 特殊字符和符号 - 多语言 Unicode 字符 ## 🛠️ Mock 说明 测试使用了以下 Mock 对象: 1. **AsyncOpenAI Client**: 模拟 OpenAI API 客户端 2. **prompt_loader**: 模拟提示词加载器 3. **progress_manager**: 模拟进度管理器 4. **ChatPromptTemplate**: 模拟提示词模板 这样可以在不依赖实际 API 服务的情况下进行测试。 ## 📝 注意事项 1. **集成测试**: 标记为 `@pytest.mark.integration` 的测试需要实际的 API 服务,默认会被跳过 2. **异步测试**: 所有涉及 async/await 的测试都使用 `@pytest.mark.asyncio` 装饰器 3. **Mock 数据**: 测试使用 Mock 数据,不会产生实际的 API 调用费用 4. **执行时间**: 某些测试包含 `asyncio.sleep()` 来模拟延迟,可能需要几秒钟 ## 🐛 调试技巧 ### 查看详细日志 ```bash pytest Semantic_Logic_Test/test_semantic_logic.py -v -s --log-cli-level=DEBUG ``` ### 只运行失败的测试 ```bash pytest Semantic_Logic_Test/test_semantic_logic.py --lf ``` ### 进入调试模式 ```bash pytest Semantic_Logic_Test/test_semantic_logic.py --pdb ``` ### 生成 HTML 报告 ```bash pytest Semantic_Logic_Test/test_semantic_logic.py --html=report.html --self-contained-html ``` ## 📈 持续集成 可以将测试集成到 CI/CD 流程中: ```yaml # .github/workflows/test.yml 示例 name: Run Tests on: [push, pull_request] jobs: test: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - name: Set up Python uses: actions/setup-python@v2 with: python-version: "3.9" - name: Install dependencies run: | pip install -r requirements.txt pip install pytest pytest-asyncio pytest-mock pytest-cov - name: Run tests run: pytest Semantic_Logic_Test/test_semantic_logic.py -v --cov ``` ## 📞 联系方式 如有问题或建议,请联系开发团队。