Java测试自动化工具

MCP Java Testing Agent是一个自动化测试生成、覆盖率分析和样式检查的工具,专为Java/Maven项目设计。

  • by amyfan7
  • Python
  • 本地部署
  • 1 星标
  • 更新于
在 GitHub 查看 amyfan7/se333-final-mcp

使用说明

MCP Java测试代理

此存储库提供了一个MCP(模型上下文协议)服务器,该服务器公开了用于自动化Java/Maven项目的测试生成、覆盖率分析和样式检查的工具。它被设计为由MCP感知客户端使用 tester.prompt.md 提示。


概述

MCP服务器(在 main.py)公开了以下工具:

  • 在Java代码库中发现公共方法
  • 生成骨架JUnit测试
  • 运行Maven(mvn clean test)
  • 分析JaCoCo报道
  • 分析Checkstyle报告

高级循环(迭代直到达到覆盖率/样式目标)在 tester.prompt.md 并且由LLM客户端执行,而不是用Python硬编码。


需求

  • Python 3.x
  • Java JDK已安装 PATH
  • Maven(mvn)已安装并打开 PATH
  • Maven项目配置为:
    • 将源放在类似下面 A2/src/main/java
    • 在以下位置生成JaCoCo XML A2/target/site/jacoco/jacoco.xml
    • 在以下位置生成Checkstyle XML A2/target/checkstyle-result.xml
  • 具有MCP功能的编辑器/客户端(例如,与MCP集成的VS代码)

安装和配置

  1. 克隆并安装:

    git clone <your-repo-url>
    cd <your-repo-folder>
    pip install fastmcp
    
     Verify:
    
     java -version
     mvn -v
    
     Ensure your Maven pom.xml is set up to:
    
         Run tests
    
         Produce JaCoCo and Checkstyle reports at the paths above
    
    

运行MCP服务器

从项目根:

python服务器.py

这将通过SSE启动MCP服务器:

如果 名字 == "主要的": mcp.run(运输=“sse”)

您的MCP客户端应配置为连接到此服务器。 使用测试仪提示

假设您在启用MCP的VS Code编辑器中:

Open the project folder (containing main.py and tester.prompt.md).

Open tester.prompt.md (or tester.md) in the editor.

Press Ctrl+Shift+P to open the Command Palette.

Select “Run Prompt” (or the equivalent command).

Choose the tester prompt.

然后,MCP客户端将:

Call the tools defined in tester.prompt.md:

- mcp-final/generate-tests
- mcp-final/run_tests
- mcp-final/analyze-coverage
- mcp-final/get_all_public_methods
- mcp-final/analyze-checkstyle
- mcp-final/run-checkstyle
- mcp-final/git-add-all
- mcp-final/git-commit
- mcp-final/git_pull_request
- mcp-final/git_push
- mcp-final/git_status

Iterate to generate tests, run coverage/style checks, and produce a final summary.

项目结构(典型)

├── main.py#MCP服务器和工具 ├── .github/提示/ │ └── tester.prompt.md#代理配置/提示 ├── A2/ │ ├── pom.xml文件 │ ├── src/main/java/。..#Java源代码 │ └── 目标/ │ ├── site/jaco/jacoco.xml │ └── checkstyle-result.xml

根据需要调整路径;默认值用于工具实现。 MCP工具 generate_tests(源文件:str)->str

为给定的.java文件生成一个骨架JUnit测试类。

Extracts the class name and public method names via regex.

Creates {ClassName}Test with stub methods:

@Test
void test_methodName() {
    // TODO: implement test
}

Writes to: codebase/src/test/java/{ClassName}Test.java.

返回:成功时的路径消息,如果找不到文件/类/方法,则返回错误字符串。 get_all_public_methods(目录:str=“A2/src/main/java”)->列表[str]

递归扫描dir中的.java文件,并返回所有以public开头的行。

Uses os.walk to find .java files.

Reads each file line-by-line and keeps line.strip().startswith("public").

返回:原始行列表(方法签名、构造函数或公共字段)。 analyze_coverage(xml_path:str=“A2/target/site/jaco/jacoco.xml”)->dict

解析JaCoCo XML报告并列出指令覆盖率<100%的方法。

输出结构:

{ “未覆盖的方法”:[ { “class”:“com/example/MClass”, “方法”:“myMethod”, “覆盖率”:0.75, “错过”:10, “覆盖”:30 }, ... ], “计数”:3 }

如果缺少XML文件,则返回“error”键。 run_checkstyle()->str

运行Maven构建和测试:

mvn清洁测试

假设您的Maven配置在构建过程中运行Checkstyle和JaCoCo。

返回:Maven进程的stdout(本文中可见错误)。 analyze_checkstyle(xml_path:str=“A2/target/checkstyle result.xml”)->字典

解析Checkstyle XML报告并列出所有样式冲突。

输出结构:

{ “违规”:[ { “file”:“/path/to/Foo.java”, “行”:“42”, “列”:“13”, “严重性”:“警告”, “message”:“行长度超过100个字符”, “源代码”:“com.puppycraw.tools.checkstyle.checks.sizes.LineLengthCheck” }, ... ], “计数”:5 }

如果缺少XML文件,则返回“error”键。 代理工作流摘要

tester.prompt.md中实现的典型工作流:

Call get_all_public_methods to discover public methods.

Call generate_tests to create initial JUnit tests.

Call run_checkstyle to run mvn clean test and produce JaCoCo + Checkstyle reports.

Call analyze_coverage to find uncovered methods.

Call analyze_checkstyle to find style violations.

Update tests and code (by the agent using file edits), then repeat steps 3–5.

Stop when:

    100% coverage and zero Checkstyle issues are reached, or

    A maximum iteration count (e.g., 10) is reached.

Produce a final summary report (tests created, coverage, style status, recommendations).

注意事项和限制

Regex-based parsing is simple and may not handle all Java edge cases (complex generics, annotations, inner classes).

Default paths (A2/src/main/java, codebase/src/test/java, etc.) can be customized; keep tool defaults in sync with your project layout.

run_checkstyle only runs mvn clean test; ensure your pom.xml actually binds JaCoCo and Checkstyle to this phase.

The iterative “improve coverage & style” loop is implemented by the MCP client/LLM using these tools, not within server.py itself.