Java代码测试代理

一个智能MCP代理,自动生成测试用例,使用JaCoCo测量代码覆盖率,识别缺失的测试,并迭代提高Java类的测试覆盖率至90%以上。

  • by SidiqaFekrat
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 SidiqaFekrat/SE333-mcp-testing-agent

使用说明

AI驱动的测试代理:

一个智能MCP代理,可以自动生成测试,使用JaCoCo测量代码覆盖率,使用覆盖率识别缺失的测试,并迭代地将java类的测试覆盖率提高到90%以上。

安装和设置:

先决条件:

    Python 3.8+
    Java 11+
    Maven 3.6+
    Node.js 18+
    Git

安装步骤:

  1. 克隆并导航到项目: cd最终项目_se333_sidiqa

  2. 创建虚拟环境: 紫外线真空 source.ven/bin/activate

  3. 安装MCP依赖项: uv添加'mcp[cli]'httpx-fastmcp

  4. 启动MCP服务器: python3代码库/server.py

跑步后,你应该看到:Uvicorn正在跑步http://127.0.0.1:8000

在VS代码中配置: 1.按CTRL+SHIFT+P搜索MCP:添加服务器 2.从运行codebase/server.py的输出中输入URL:http://127.0.0.1:8000/sse 3.名称:检测试剂 4.启用聊天:在VS代码设置中自动批准 5.刷新VS Code Chat查看应该可见的MCP工具

MCP工具文档:

文件:codebase/server.py(17个工具)

阶段1-2:测试生成和覆盖。

1. analyze_java_code(file_path):
    Input: Java file path.
    Output: Methods with signatures
    Purpose: Extract methods for testing

2. generate_test_template(class_name, methods)
    Input: Class name plus methods dict
    Output: JUnit test class code
    Purpose: generate Unit tests

3. run_maven_tests(project_path)
    Input: Project path
    Output: Test results with the coverage info
    Purpose: Get coverage metrics

4. total_coverage(project_path)
    Input: Project path
    Output:Line/branch/method/instruction %
    Purpose: Get coverage info 


5. missing_coverage(file_path, project_path)
    Input: File path + project path
    Output: List of uncovered lines
    Purpose: Identify missing coverage if there is any

6. find_jacoco_path(project_path)
    Input: Project path
    Output: Path to jacoco.xml
    Purpose: Locate coverage report

第三阶段:Git自动化

1. git_status(repo_path)
    Input: Repo path
    Output: Staged / unstaged changes
    Purpose: Check repository status

2. git_add_all(repo_path)
    Input: Repo path
    Output: Stage the changes in the java files and the new added tests by the agent
    Purpose: Stage changes (excludes build artifacts)

3. git_commit(repo_path, message, coverage)
    Input: Repo path + message + coverage dict
    Output: commit the changes and the coverage result
    Purpose: Commit with coverage stats

4. git_push(repo_path, remote, branch)
    Input: Repo path + remote + branch
    Output: Push result
    Purpose: Push the commited changes to remote repo

5. git_pull_request(repo_path, base, title, body, coverage)
    Input: Repo path + base + metadata
    Output: PR URL or manual instructions
    Purpose: Create pull request

第五阶段:创意延伸

1. generate_specification_tests(java_file_path, class_name)
    Input: Java file path + class name
    Output: Test class with boundary, equivalence, and decision table tests
    Purpose: Advanced specification-based testing

2. code_review(file_path)
    Input: Java file path
    Output: Issues list with auto-fixes
    Purpose: Detects code smells, security issues, style violations

代理工作流:

文件:tester.prompt.md

步骤1-9(阶段4-5:覆盖迭代):

1. Analyze all the source files.
2. Generate tests for them.
3. Run tests with JaCoCo to get the coverage info.
4. Fix error if needed.
5. Locate the coverage report.
6. parse the coverage metrics.
7. Identify uncovered lines
8. Generate additional tests if there is missing coverage.
9. Repeat until 90%+ coverage
10. Generate specification-based tests
11. Review code quality
12. Check git status
13. Stage files
14. Commit with coverage stats
15. Push to origin
16. Create PR against main

使用示例:

打开VS Code聊天并键入(运行#tester.prompt.md)

故障排除:

  1. 问题:使用uv时,添加mcp[cli]httpx-fastmcp,并显示“未找到匹配项:mcp[cri]”

    解决方案:运行uv-add“mcp[cli]”httpx-fastmcp

  2. 问题:未找到JaCoCo报告

    解决方案:运行mvn clean测试jacoco:代码库中的报告/

  3. 问题:MCP服务器无法启动

    解决方案:验证:python3-c“import fastmcp”工作正常

  4. 问题:聊天中未显示工具

    解决方案:重新启动VS Code并重新连接MCP服务器