Running Tests
Relevant source files
The following files were used as context for generating this wiki page:The testing process for the MCP Server is designed to validate the functionality and reliability of its API endpoints. It utilizes the pytest framework to discover and run tests, and httpx to perform asynchronous HTTP requests against the FastAPI application. This ensures that endpoints, such as those defined in mcp_server/main.py, behave as expected.
The overall testing strategy is centered on API-level integration tests that check status codes and JSON responses for correctness. This approach verifies the end-to-end behavior of each endpoint from the perspective of a client.
Sources: mcp-server/tests/test_server.py, mcp-server/pyproject.toml, mcp-server/mcp_server/main.py
Test Environment Setup
To run the tests, the development dependencies must be installed. These are defined in the pyproject.toml file and are essential for the testing toolchain.
Sources: mcp-server/pyproject.toml:18-22
The command to install all necessary project and development dependencies is:
poetry install
Sources: mcp-server/README.md:16
Testing Dependencies
The following libraries are required for the test environment:
| Dependency | Version | Description |
|---|---|---|
pytest | ^7.4.2 | The framework for writing and executing tests. |
httpx | ^0.25.0 | An asynchronous HTTP client used to make requests to the API endpoints. |
pytest-asyncio | ^0.21.1 | A pytest plugin to handle asynchronous test functions. |
Sources: mcp-server/pyproject.toml:19-21
Test Configuration
Test execution is configured within the pyproject.toml file under the [tool.pytest.ini_options] section. This configuration dictates how pytest discovers and runs tests.
Sources: mcp-server/pyproject.toml:24-29
[tool.pytest.ini_options]
minversion = "6.0"
addopts = "-ra -q"
testpaths = [
"tests",
]
python_files = "test_*.py"
Sources: mcp-server/pyproject.toml:24-29
Configuration Options
| Option | Value | Description |
|---|---|---|
minversion | "6.0" | Specifies the minimum required version of pytest. |
addopts | "-ra -q" | Default command-line options for pytest (-r a for summary, -q for quiet). |
testpaths | ["tests"] | The directory where pytest will look for tests. |
python_files | "test_*.py" | The naming pattern for files that contain tests. |
Sources: mcp-server/pyproject.toml:25-29
The application itself is configured via the mcp_server/config.py file, which loads settings from environment variables. The tests import the configured app instance from mcp_server/main.py to ensure they are testing against the correct application setup.
Sources: mcp-server/mcp_server/config.py, mcp-server/mcp_server/main.py:4, mcp-server/tests/test_server.py:6
Executing Tests
To run the entire test suite, execute the following command from the mcp-server directory:
poetry run pytest
Sources: mcp-server/README.md:22
This command initiates the test execution flow, which is governed by the pytest configuration.
The diagram below illustrates the test execution process.
Test Implementation
Tests are implemented as asynchronous functions decorated with @pytest.mark.asyncio. The httpx.AsyncClient is used to interact with the FastAPI application instance.
Sources: mcp-server/tests/test_server.py:8-14
Example Test: test_root
The test_root function in test_server.py serves as a template for testing API endpoints. It sends a GET request to the root (/) endpoint and validates the response.
# mcp-server/tests/test_server.py
import pytest
from httpx import AsyncClient
from mcp_server.main import app
@pytest.mark.asyncio
async def test_root():
async with AsyncClient(app=app, base_url="http://test") as ac:
response = await ac.get("/")
assert response.status_code == 200
assert response.json() == {"message": "MCP Server is running"}
Sources: mcp-server/tests/test_server.py:8-14
The following sequence diagram shows the interactions during the test_root execution.
Conclusion
The testing framework for the MCP Server provides a robust and straightforward way to ensure API reliability. By using pytest for orchestration, pyproject.toml for configuration, and httpx for asynchronous requests, the test suite can efficiently validate endpoint behavior. This setup is critical for maintaining code quality and preventing regressions as the application evolves.