Skip to content

[Enhancement] Add Test Examples for MCP Tools #50

Description

@farce1

Enhancement Request

The MCP server template doesn't include test examples for MCP tools. Adding comprehensive test examples would help users write reliable MCP servers.

Proposed Test Structure

1. Basic Tool Testing

# tests/test_hello.py
import pytest
from app.tools.hello import hello

@pytest.mark.asyncio
async def test_hello_valid_input():
    result = await hello("World")
    assert result == "Hello, World!"

@pytest.mark.asyncio
async def test_hello_empty_input():
    with pytest.raises(ValueError):
        await hello("")

@pytest.mark.asyncio
async def test_hello_special_characters():
    result = await hello("Claude 3.5!")
    assert result == "Hello, Claude 3.5!!"

2. MCP Server Testing

# tests/test_mcp_server.py
import pytest
from fastmcp.testing import MCPTestClient
from app.main import mcp

@pytest.fixture
def mcp_client():
    return MCPTestClient(mcp)

@pytest.mark.asyncio
async def test_tool_registration(mcp_client):
    tools = await mcp_client.list_tools()
    assert "hello" in [tool.name for tool in tools]

@pytest.mark.asyncio
async def test_tool_execution(mcp_client):
    result = await mcp_client.call_tool("hello", {"name": "Test"})
    assert result == "Hello, Test!"

3. Integration Testing

# tests/test_integration.py
@pytest.mark.asyncio
async def test_multiple_tool_calls(mcp_client):
    # Test calling multiple tools in sequence
    pass

@pytest.mark.asyncio
async def test_error_propagation(mcp_client):
    # Test that errors are properly propagated
    pass

4. Mock and Fixture Examples

# tests/conftest.py
import pytest
from unittest.mock import AsyncMock

@pytest.fixture
def mock_database():
    """Mock database for testing."""
    db = AsyncMock()
    db.query.return_value = [{"id": 1, "name": "Test"}]
    return db

@pytest.fixture
def mock_api_client():
    """Mock API client for testing."""
    client = AsyncMock()
    client.get.return_value = {"status": "success"}
    return client

5. Test Configuration

# pytest.ini or pyproject.toml
[tool.pytest.ini_options]
testpaths = ["tests"]
asyncio_mode = "auto"
python_files = "test_*.py"
python_functions = "test_*"

Test Categories to Include

  1. Unit Tests: Individual tool functions
  2. Integration Tests: Multiple tools working together
  3. Error Handling Tests: Edge cases and error scenarios
  4. Performance Tests: Response time and resource usage
  5. Security Tests: Input validation and sanitization

Benefits

  • Ensures code reliability
  • Prevents regressions
  • Documents expected behavior
  • Facilitates refactoring
  • Improves code quality

Implementation

  1. Add tests/ directory to template
  2. Include example test files
  3. Add testing dependencies to pyproject.toml
  4. Include GitHub Actions workflow for CI/CD
  5. Add test coverage reporting

Dependencies to Add

[project.optional-dependencies]
test = [
    "pytest>=7.0.0",
    "pytest-asyncio>=0.21.0",
    "pytest-cov>=4.0.0",
    "pytest-mock>=3.10.0",
]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions