Expand description
Test harness infrastructure for FastMCP.
This module provides utilities for writing comprehensive tests without mocks:
TestContext: Wrapper aroundCx::for_testing()with helper methodsTestServer: Builder for creating test servers with real handlersTestClient: Client for in-process testing with MemoryTransport- Assertion helpers for validating JSON-RPC and MCP compliance
- Timing utilities for performance measurements
fixtures: Test data generators for tools, resources, prompts, and messages
§Example
ⓘ
use fastmcp_rust::testing::prelude::*;
#[test]
fn test_tool_call() {
let ctx = TestContext::new();
// Create a test server with real handlers
let (router, client_transport, server_transport) = TestServer::builder()
.build();
// Create a test client
let mut client = TestClient::new(client_transport);
client.initialize().unwrap();
// Call tool and verify
let result = client.call_tool("my_tool", json!({"arg": "value"}));
assert!(result.is_ok());
}§Design Philosophy
- No mocks: All tests use real implementations via MemoryTransport
- Deterministic: Uses asupersync Lab runtime for reproducibility
- Comprehensive logging: Built-in trace support for debugging
- Resource cleanup: Automatic cleanup of spawned tasks
Modules§
Structs§
- Method
Timing - Timing statistics for a method.
- Span
- A span for tracking nested operations.
- Stopwatch
- A stopwatch for measuring elapsed time with lap support.
- Test
Client - Test client for in-process MCP testing.
- Test
Context - Test context wrapper providing convenient testing utilities.
- Test
Server - Convenience wrapper for
TestServerBuilder. - Test
Server Builder - Builder for creating test servers with real handlers.
- Test
Trace - Main test trace collector.
- Test
Trace Builder - Builder for creating test traces.
- Test
Trace Output - Complete trace of a test execution.
- Timer
- A simple timer that fires after a duration.
- Timing
Stats - Timing statistics for a set of measurements.
- Trace
Retention Config - Configuration for trace file cleanup.
- Trace
Summary - Summary statistics for a trace.
Enums§
- Trace
Entry - Entry type in a trace.
- Trace
Level - Log level for trace entries.
Functions§
- assert_
content_ valid - Validates that content is MCP-compliant.
- assert_
is_ notification - Validates that a JSON-RPC request is a valid notification.
- assert_
is_ request - Validates that a JSON-RPC request is a valid request (not notification).
- assert_
json_ rpc_ error - Validates that a JSON-RPC response indicates an error.
- assert_
json_ rpc_ success - Validates that a JSON-RPC response indicates success.
- assert_
json_ rpc_ valid - Validates that a JSON-RPC message is well-formed.
- assert_
mcp_ compliant - Validates that an MCP response is compliant with the protocol.
- assert_
prompt_ valid - Validates that a prompt definition is MCP-compliant.
- assert_
resource_ valid - Validates that a resource definition is MCP-compliant.
- assert_
tool_ valid - Validates that a tool definition is MCP-compliant.
- cleanup_
old_ traces - Cleans up old trace files based on retention configuration.
- get_
trace_ dir - Returns the trace output directory from
FASTMCP_TEST_TRACE_DIRor defaults to “test_traces”. - is_
trace_ enabled - Check if test tracing is enabled via environment variable.
- measure_
duration - Measures the duration of a closure execution.