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.

2.3 KiB

Cppcheck Test Generator

一个用于根据 cppcheck 报告生成可运行测试用例的模块化工具包。

模块结构

models.py - 数据模型

  • CppcheckIssue: cppcheck 问题信息
  • IssueLocation: 问题位置信息
  • CodeContext: 代码上下文信息

parsers.py - 解析器模块

  • parse_cppcheck_xml(): 解析 XML 格式的 cppcheck 报告
  • parse_cppcheck_text(): 解析文本格式的 cppcheck 报告
  • read_code_snippet(): 读取代码片段

analysis.py - 代码分析模块

  • analyze_code_context(): 分析代码上下文
  • analyze_issue_relevance(): 分析问题相关性
  • analyze_project_structure(): 分析项目结构
  • get_enhanced_issue_analysis(): 获取增强的问题分析
  • filter_and_clean_issues(): 过滤和清理问题
  • prioritize_issues(): 问题优先级排序

generation.py - 测试用例生成模块

  • generate_test_for_issue(): 使用 AI 生成测试用例
  • get_issue_specific_template(): 获取问题特定的模板
  • smart_select_issues(): 智能选择最有代表性的问题
  • write_issue_output(): 写入问题输出文件

verification.py - 验证模块

  • verify_single_test(): 验证单个测试用例
  • auto_verify_tests(): 自动验证所有测试用例
  • generate_verification_report(): 生成验证报告

main.py - 主程序入口

  • main(): 主程序逻辑,处理命令行参数和协调各模块

使用方法

作为模块使用

from cppcheck_test_generator import main
main.main(['report.xml', '--out', 'tests', '--max', '5'])

作为脚本使用

python cppcheck_to_tests_new.py report.xml --out tests --max 5

功能特性

  • 支持 XML 和文本格式的 cppcheck 报告
  • 智能分析代码上下文和项目结构
  • AI 驱动的测试用例生成
  • 自动验证生成的测试用例
  • 智能筛选最有代表性的问题
  • 生成详细的验证报告

依赖

  • Python 3.7+
  • OpenAI API 客户端(用于 AI 生成)
  • g++ 编译器(用于验证)
  • cppcheck用于验证

迁移说明

从原始的 cppcheck_to_tests.py 迁移到新的模块化版本:

  1. 所有功能保持不变
  2. 命令行参数完全兼容
  3. 输出格式保持一致
  4. 性能没有变化

新的模块化结构提供了更好的:

  • 代码可维护性
  • 功能扩展性
  • 测试覆盖性
  • 文档完整性