Pytest MCP Server

Pytest MCP Server

Enables AI assistants to run and analyze pytest tests for desktop applications through interactive commands. Supports test execution, filtering, result analysis, and debugging for comprehensive test automation workflows.

Category
访问服务器

README

Pytest MCP Server

A Model Context Protocol (MCP) server that enables AI assistants like Claude to run and analyze pytest tests for desktop applications.

Overview

This project demonstrates how to integrate pytest with AI assistants through MCP. It includes:

  • Real Desktop Application: A fully functional calculator app built with tkinter
  • Comprehensive Test Suite: pytest tests covering UI, functionality, and edge cases
  • MCP Server: Connects pytest to AI assistants for interactive testing

Project Structure

pytest-mcp-server/
├── src/
│   ├── __init__.py
│   └── calculator_app.py      # Desktop calculator application
├── tests/
│   ├── __init__.py
│   └── test_calculator.py     # Pytest test suite
├── server.py                   # MCP server implementation
├── pyproject.toml             # Project configuration
└── README.md

Installation

Prerequisites

  • Python 3.10 or higher
  • pip package manager

Setup

  1. Clone or navigate to the project directory:
cd /Users/303748/Documents/ws/pytest-mcp-server
  1. Create a virtual environment:
python3 -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate
  1. Install dependencies:
pip install -e .

Or install with development dependencies:

pip install -e ".[dev]"

Running the Calculator App

To run the desktop calculator application:

python -m src.calculator_app

Or use the installed script:

calculator-app

Running Tests Manually

Run all tests:

pytest tests/

Run with verbose output:

pytest tests/ -v

Run only UI tests:

pytest tests/ -m ui

Run tests excluding slow ones:

pytest tests/ -m "not slow"

Run specific test class:

pytest tests/test_calculator.py::TestBasicOperations

Using the MCP Server

Configuration

  1. Find your Claude Desktop config file:

    • macOS: ~/Library/Application Support/Claude/claude_desktop_config.json
    • Windows: %APPDATA%\Claude\claude_desktop_config.json
  2. Add the MCP server configuration:

IMPORTANT NOTICE: You need to give execution rights to the run_server.sh file

{
  "mcpServers": {
    "pytest": {
      "command": "/Users/303748/Documents/ws/pytest-mcp-server/run_server.sh",
      "args": []
    }
  }
}
  1. Restart Claude Desktop

Using with Claude

Once configured, you can interact with your tests through Claude:

Example conversations:

You: "List all available tests in the calculator project"

You: "Run the UI tests for the calculator"

You: "Run only the error handling tests"

You: "Show me the results of the last test run"

You: "The addition test is failing, can you help me debug it?"

MCP Server Tools

The server provides three main tools:

1. run_tests

Executes pytest tests with specified options.

Parameters:

  • test_path (required): Path to test file or directory
  • markers (optional): Pytest markers to filter tests
  • verbose (optional): Show detailed output (default: true)
  • capture (optional): Output capture method (default: "no")

2. list_tests

Lists all available tests without running them.

Parameters:

  • test_path (optional): Path to scan for tests (default: "tests/")

3. get_test_results

Retrieves and formats the last test run results.

Parameters: None

Test Markers

The test suite uses these markers:

  • @pytest.mark.ui - UI-related tests
  • @pytest.mark.error_handling - Error handling tests
  • @pytest.mark.slow - Slow-running tests

Development

Running Tests with Coverage

pytest tests/ --cov=src --cov-report=html

View coverage report:

open htmlcov/index.html  # macOS

Code Formatting

Format code with Black:

black src/ tests/ server.py

Lint code with Ruff:

ruff check src/ tests/ server.py

Calculator Features

The calculator application supports:

  • Basic operations: Addition, subtraction, multiplication, division
  • Advanced operations: Square root
  • UI controls: Clear (C), Clear Entry (CE), Backspace (←)
  • Decimal numbers: Full decimal support
  • Error handling: Division by zero, negative square roots, invalid expressions

Test Coverage

The test suite includes:

Basic Operations

  • Addition
  • Subtraction
  • Multiplication
  • Division
  • Division by zero handling

Advanced Operations

  • Square root calculations
  • Square root of negative numbers
  • Decimal number operations

UI Functions

  • Clear button functionality
  • Clear entry functionality
  • Backspace operation
  • Chaining multiple operations

Edge Cases

  • Very large numbers
  • Operations with zero
  • Invalid expressions

Troubleshooting

Tests not running

Make sure you're in the project directory and have activated the virtual environment:

cd /Users/303748/Documents/ws/pytest-mcp-server
source venv/bin/activate
pytest tests/

MCP server not connecting

  1. Check that the path in claude_desktop_config.json is correct
  2. Ensure Python is in your PATH
  3. Restart Claude Desktop after configuration changes
  4. Check Claude Desktop logs for error messages

Import errors

Make sure the package is installed in editable mode:

pip install -e .

Contributing

Contributions are welcome! Areas for improvement:

  • Add more calculator features (memory, history)
  • Implement screenshot capture on test failures
  • Add performance benchmarking tools
  • Create more test scenarios
  • Improve error messages

Resources

Author

Created as an example project for the LinkedIn article "Building an MCP Server for Pytest: Automating Desktop Application Testing"

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选