M

MCP测试运行器

@privsim/mcp-test-runner
1 Stars 172 次浏览 privsim 更新于 2026-08-23

通过模型上下文协议接口,促进对各种测试框架(包括 Bats、Pytest、Flutter、Jest 和 Go)的统一执行和结果解析。

该服务暂未提供标准配置,请参考 README 手动接入

服务介绍

测试运行器 MCP

一个用于运行和解析来自多个测试框架的测试结果的模型上下文协议(MCP)服务器。该服务器提供了一个统一的接口来执行测试并处理其输出,支持以下框架:

  • Bats (Bash 自动化测试系统)
  • Pytest (Python 测试框架)
  • Flutter 测试
  • Jest (JavaScript 测试框架)
  • Go 测试
  • Rust 测试 (Cargo test)
  • 通用 (用于任意命令执行)

安装

npm install test-runner-mcp

先决条件

对于各自的测试类型,需要安装以下测试框架:

使用方法

配置

将测试运行器添加到您的 MCP 设置中(例如,在 claude_desktop_config.jsoncline_mcp_settings.json 中):

{
  "mcpServers": {
    "test-runner": {
      "command": "node",
      "args": ["/path/to/test-runner-mcp/build/index.js"],
      "env": {
        "NODE_PATH": "/path/to/test-runner-mcp/node_modules",
        // Flutter-specific environment (required for Flutter tests)
        "FLUTTER_ROOT": "/opt/homebrew/Caskroom/flutter/3.27.2/flutter",
        "PUB_CACHE": "/Users/username/.pub-cache",
        "PATH": "/opt/homebrew/Caskroom/flutter/3.27.2/flutter/bin:/usr/local/bin:/usr/bin:/bin"
      }
    }
  }
}

注意:对于 Flutter 测试,请确保您替换:

  • /opt/homebrew/Caskroom/flutter/3.27.2/flutter 为您的实际 Flutter 安装路径
  • /Users/username/.pub-cache 为您的实际 pub 缓存路径
  • 更新 PATH 以包含您系统的实际路径

您可以运行以下命令来找到这些值:

# Get Flutter root
flutter --version

# Get pub cache path
echo $PUB_CACHE   # or default to $HOME/.pub-cache

# Get Flutter binary path
which flutter

运行测试

使用 run_tests 工具,并带有以下参数:

{
  "command": "test command to execute",
  "workingDir": "working directory for test execution",
  "framework": "bats|pytest|flutter|jest|go|rust|generic",
  "outputDir": "directory for test results",
  "timeout": "test execution timeout in milliseconds (default: 300000)",
  "env": "optional environment variables",
  "securityOptions": "optional security options for command execution"
}

每个框架的示例:

// Bats
{
  "command": "bats test/*.bats",
  "workingDir": "/path/to/project",
  "framework": "bats",
  "outputDir": "test_reports"
}

// Pytest
{
  "command": "pytest test_file.py -v",
  "workingDir": "/path/to/project",
  "framework": "pytest",
  "outputDir": "test_reports"
}

// Flutter
{
  "command": "flutter test test/widget_test.dart",
  "workingDir": "/path/to/project",
  "framework": "flutter",
  "outputDir": "test_reports",
  "FLUTTER_ROOT": "/opt/homebrew/Caskroom/flutter/3.27.2/flutter",
  "PUB_CACHE": "/Users/username/.pub-cache",
  "PATH": "/opt/homebrew/Caskroom/flutter/3.27.2/flutter/bin:/usr/local/bin:/usr/bin:/bin"
}

// Jest
{
  "command": "jest test/*.test.js",
  "workingDir": "/path/to/project",
  "framework": "jest",
  "outputDir": "test_reports"
}

// Go
{
  "command": "go test ./...",
  "workingDir": "/path/to/project",
  "framework": "go",
  "outputDir": "test_reports"
}

// Rust
{
  "command": "cargo test",
  "workingDir": "/path/to/project",
  "framework": "rust",
  "outputDir": "test_reports"
}

// Generic (for arbitrary commands, CI/CD tools, etc.)
{
  "command": "act -j build",
  "workingDir": "/path/to/project",
  "framework": "generic",
  "outputDir": "test_reports"
}

// Generic with security overrides
{
  "command": "sudo docker-compose -f docker-compose.test.yml up",
  "workingDir": "/path/to/project",
  "framework": "generic",
  "outputDir": "test_reports",
  "securityOptions": {
    "allowSudo": true
  }
}

安全特性

测试运行器包括内置的安全特性,以防止执行潜在有害的命令,特别是对于 generic 框架:

  1. 命令验证

    • 默认阻止 sudosu
    • 防止危险命令如 rm -rf /
    • 阻止文件系统写操作超出安全位置
  2. 环境变量清理

    • 过滤掉潜在危险的环境变量
    • 防止覆盖关键系统变量
    • 确保安全的路径处理
  3. 可配置的安全性

    • 在必要时通过 securityOptions 覆盖安全限制
    • 对安全特性进行细粒度控制
    • 标准测试使用的默认安全设置

您可以配置的安全选项:

{
  "securityOptions": {
    "allowSudo": false,        // Allow sudo commands
    "allowSu": false,          // Allow su commands
    "allowShellExpansion": true, // Allow shell expansion like $() or backticks
    "allowPipeToFile": false   // Allow pipe to file operations (> or >>)
  }
}

Flutter 测试支持

测试运行器包括对 Flutter 测试的增强支持:

  1. 环境设置

    • 自动配置 Flutter 环境
    • 设置 PATH 和 PUB_CACHE
    • 验证 Flutter 安装
  2. 错误处理

    • 收集堆栈跟踪
    • 处理断言错误
    • 捕获异常
    • 检测测试失败
  3. 输出处理

    • 完整捕获测试输出
    • 保留堆栈跟踪
    • 详细错误报告
    • 保留原始输出

Rust 测试支持

测试运行器为 Rust 的 cargo test 提供特定支持:

  1. 环境设置

    • 自动设置 RUST_BACKTRACE=1 以获得更好的错误信息
  2. 输出解析

    • 解析单个测试结果
    • 捕获失败测试的详细错误信息
    • 识别被忽略的测试
    • 提取摘要信息

通用测试支持

对于 CI/CD 流水线、通过 act 的 GitHub Actions 或任何其他命令执行,通用框架提供:

  1. 自动输出分析

    • 尝试将输出分割成逻辑块
    • 识别节标题
    • 检测通过/失败指示符
    • 即使对于未知格式也能提供合理的输出结构
  2. 灵活集成

    • 支持任意 shell 命令
    • 不需要特定的格式要求
    • 非常适合与 act、Docker 和自定义脚本等工具集成
  3. 安全特性

    • 命令验证以防止有害操作
    • 可配置为在必要时允许特定的提升权限

输出格式

测试运行器生成结构化的输出,同时保留完整的测试输出:

interface TestResult {
  name: string;
  passed: boolean;
  output: string[];
  rawOutput?: string;  // Complete unprocessed output
}

interface TestSummary {
  total: number;
  passed: number;
  failed: number;
  duration?: number;
}

interface ParsedResults {
  framework: string;
  tests: TestResult[];
  summary: TestSummary;
  rawOutput: string;  // Complete command output
}

结果保存在指定的输出目录中:

  • test_output.log:原始测试输出
  • test_errors.log:如果有错误消息
  • test_results.json:结构化的测试结果
  • summary.txt:人类可读的摘要

开发

设置

  1. 克隆仓库
  2. 安装依赖项:
    npm install
    
  3. 构建项目:
    npm run build
    

运行测试

npm test

测试套件包括所有受支持框架的测试,并验证成功和失败的测试场景。

CI/CD

该项目使用 GitHub Actions 进行持续集成:

  • 在 Node.js 18.x 和 20.x 上进行自动化测试
  • 测试结果作为工件上传
  • 配置 Dependabot 用于自动更新依赖项

贡献

  1. 分叉仓库
  2. 创建你的功能分支
  3. 提交你的更改
  4. 推送到分支
  5. 创建 Pull Request

许可证

该项目根据 MIT 许可证许可 - 请参阅 LICENSE 文件获取详细信息。

相关 MCP 服务