Skip to main content

Module testing

Module testing 

Source
Expand description

Test harness infrastructure for FastMCP.

This module provides utilities for writing comprehensive tests without mocks:

  • TestContext: Wrapper around Cx::for_testing() with helper methods
  • TestServer: Builder for creating test servers with real handlers
  • TestClient: 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§

fixtures
Test fixture generators for FastMCP.
prelude
Prelude for convenient imports in tests.

Structs§

MethodTiming
Timing statistics for a method.
Span
A span for tracking nested operations.
Stopwatch
A stopwatch for measuring elapsed time with lap support.
TestClient
Test client for in-process MCP testing.
TestContext
Test context wrapper providing convenient testing utilities.
TestServer
Convenience wrapper for TestServerBuilder.
TestServerBuilder
Builder for creating test servers with real handlers.
TestTrace
Main test trace collector.
TestTraceBuilder
Builder for creating test traces.
TestTraceOutput
Complete trace of a test execution.
Timer
A simple timer that fires after a duration.
TimingStats
Timing statistics for a set of measurements.
TraceRetentionConfig
Configuration for trace file cleanup.
TraceSummary
Summary statistics for a trace.

Enums§

TraceEntry
Entry type in a trace.
TraceLevel
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_DIR or 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.