简介
提供单元/集成/E2E测试模式指导,包含AAA结构、测试替身、参数化测试等最佳实践,S+级纯文档安全
原始描述
Unit, integration, and E2E testing patterns with framework-specific guidance. Use when asked to "write tests", "add test coverage", "testing strategy", "test this function", "create test suite", "fix flaky tests", or "improve test quality".
通过你的 Agent 添加
安装目录:testing-patterns/(目录名取自 SKILL.md 的 frontmatter name,不是 URL 里的 slug)。
点「快速安装」复制一段指令贴给你的 Agent:它会先下载到 /tmp、把文件清单和校验值给你看,你确认之后才解压安装。不会一步装完、也不改系统其它位置。
或者手动安装
包是「根级 SKILL.md + 作者附带的文件」,解压到上面的 <slug>/ 目录即可;
目录名不用换,也不要把包直接解压到 skills 根目录。
技能申请的权限由作者决定:装之前先看清单里有没有脚本、它会读写什么。
技能正文
name: testing-patterns
description: >
This skill should be used when the user asks about "testing",
"test patterns", "write tests", "unit test", "e2e test",
"integration test", "McpTestClient", "TestServerManager",
"compliance test", "test server", "test fixture", "doctest",
"cargo test", "test organization", "SSE testing",
or "test consolidation". Covers unit testing, E2E testing,
compliance testing, SSE testing, and test organization
in the Turul MCP Framework (Rust). McpTestClient is the
in-process test harness; for the production client API
see mcp-client-patterns.
Testing Patterns — Turul MCP Framework
Spec lane: Unit testing is spec-agnostic and applies to both lanes. The E2E harness (tests/shared's McpTestClient + TestServerManager) is 2025-11-25-only — verified by exhaustive grep, there is zero 2026-07-28/2026_07_28 reference anywhere under tests/, and McpTestClient::initialize() hardcodes the initialize/Mcp-Session-Id handshake this branch's stateless core removed. For 2026-07-28 E2E coverage today, drive a TestServerManager-started server with the production turul_mcp_client::McpClientBuilder (bilingual, negotiates via server/discover) instead — see 2026-07-28 E2E Testing below. The compliance suite (crates/turul-mcp-protocol-2026-07-28/src/compliance/) is separate again — schema/fixture conformance, not a live-server E2E harness.
The framework uses three testing layers: unit tests for individual components, E2E tests for full HTTP round-trips, and compliance tests for MCP specification conformance.
When to Write Tests
What are you testing?
├─ Single tool/resource/prompt logic ────────→ Unit test (#[tokio::test])
├─ Full request→response over HTTP ─────────→ E2E test (TestServerManager + McpTestClient)
├─ MCP specification conformance ───────────→ Compliance test (cargo test --test compliance)
└─ API surface / doc examples ──────────────→ Doctest (```rust block in doc comments)
What are you testing?
├─ Single tool/resource/prompt logic ────────→ Unit test (#[tokio::test])
├─ Full request→response over HTTP ─────────→ E2E test (TestServerManager + McpTestClient)
├─ MCP specification conformance ───────────→ Compliance test (cargo test --test compliance)
└─ API surface / doc examples ──────────────→ Doctest (```rust block in doc comments)
Default to unit tests. Only use E2E when you need to verify HTTP transport, session management, or middleware behavior.
Unit Testing Patterns
Unit tests exercise tools, resources, and prompts directly — no HTTP server needed.
// turul-mcp-server v0.4
use serde_json::json;
use turul_mcp_server::prelude::*;
#[derive(McpTool, Default)]
#[tool(name = "double", description = "Double a number", output = f64)]
struct DoubleTool {
#[param(description = "Number to double")]
n: f64,
}
impl DoubleTool {
async fn execute(&self, _session: Option<SessionContext>) -> McpResult<f64> {
Ok(self.n * 2.0)
}
}
#[tokio::test]
async fn test_double_tool() {
let tool = DoubleTool { n: 21.0 };
// Call directly — framework handles JSON conversion in production
let result = tool.call(json!({"n": 21.0}), None).await.unwrap();
let value: f64 = serde_json::from_value(result).unwrap();
assert_eq!(value, 42.0);
}
Key points:
- Use
tool.call(json!({...}), None)to invoke with no session context - Pass
Some(session)when testing session-dependent behavior - The
call()method is the framework-native API — avoid raw JSON-RPC request construction in unit tests
See: examples/unit-test-tool.rs for a complete example.
Asserting Tool Annotations in tools/list
Tool annotations serialize with camelCase keys and are omitted when unset — unchanged between 2025-11-25 and 2026-07-28. Test both presence and absence to prevent wire-shape regressions:
use turul_mcp_server::prelude::*;
#[derive(McpTool, Default)]
#[tool(name = "delete_file", description = "Delete a file",
read_only = false, destructive = true, idempotent = true, open_world = false)]
struct DeleteFileTool {
#[param(description = "Path")]
path: String,
}
// Tool with NO annotations — verify omission
#[derive(McpTool, Default)]
#[tool(name = "plain", description = "A plain tool")]
struct PlainTool {
#[param(description = "Value")]
value: String,
}
#[tokio::test]
async fn test_annotations_wire_shape() {
// Annotated tool → camelCase keys present
let tool = DeleteFileTool::default().to_tool();
let json = serde_json::to_value(&tool).unwrap();
let ann = &json["annotations"];
assert_eq!(ann["readOnlyHint"], false); // camelCase, not read_only_hint
assert_eq!(ann["destructiveHint"], true);
assert_eq!(ann["idempotentHint"], true);
assert_eq!(ann["openWorldHint"], false);
// Unannotated tool → annotations key absent
let plain = PlainTool::default().to_tool();
let json = serde_json::to_value(&plain).unwrap();
assert!(json.get("annotations").is_none()); // omitted, not null
}
Note:
ToolAnnotationsusesskip_serializing_if = "Option::is_none"on all fields, so unset hints don't appear in the JSON at all. This is distinct from resource/promptAnnotations(which haveaudience/priorityfields).
E2E Testing Architecture (2025-11-25 — tests/shared)
E2E tests start a real HTTP server and send requests via McpTestClient. This entire harness targets 2025-11-25's stateful handshake.
TestServerManager::start("tools-test-server")
→ find_available_port() # OS ephemeral port allocation
→ auto-build binary if missing # cargo build --package <package>
→ spawn child process # Command::new(binary_path)
→ health check loop # POST /mcp with initialize
→ return TestServerManager { port }
McpTestClient::new(port)
→ initialize() # POST initialize → capture Mcp-Session-Id
→ send_initialized_notification() # POST notifications/initialized (strict mode)
→ list_tools() / call_tool() # POST with session header
TestServerManager itself (port allocation, binary build, process spawn/kill-on-Drop) is spec-agnostic and reusable for a 2026-07-28 E2E test — swap only the client. See 2026-07-28 E2E Testing below.
See: examples/e2e-test-server.rs for a complete 2025-11-25 example.
McpTestClient API (2025-11-25 — tests/shared)
The test client manages session state (session ID capture, header injection) automatically. This API assumes the 2025-11-25 handshake throughout.
| Method | Purpose |
|---|---|
initialize() |
Send initialize with default capabilities, capture session ID |
initialize_with_capabilities(caps) |
Initialize with specific client capabilities |
send_initialized_notification() |
Complete the strict lifecycle handshake |
list_tools() / list_resources() / list_prompts() |
List registered components |
call_tool(name, args) |
Invoke a tool with JSON arguments |
call_tool_with_sse(name, args) |
Invoke a tool with Accept: text/event-stream for progress |
read_resource(uri) |
Read a resource by URI |
get_prompt(name, args) |
Get a prompt with optional arguments |
connect_sse() |
Open a GET SSE stream for real-time notifications — the endpoint this targets is unconditionally 405 on 2026-07-28 |
make_request(method, params, id) |
Generic JSON-RPC request |
send_notification(notification) |
Send a notification (no response expected) |
session_id() |
Get the current session ID |
See: references/test-utilities-reference.md for the full API with signatures and return types.
2026-07-28 E2E Testing
No equivalent of McpTestClient exists for the stateless core yet. Use the production turul-mcp-client crate (bilingual by default) against a TestServerManager-started server — it correctly negotiates 2026-07-28 via server/discover rather than assuming initialize:
// turul-mcp-server v0.4 / turul-mcp-client v0.4
use turul_mcp_client::McpClientBuilder;
let server = TestServerManager::start("tools-test-server").await?; // spec-agnostic
let client = McpClientBuilder::new()
.with_url(&format!("http://127.0.0.1:{}/mcp", server.port()))?
.build();
client.connect().await?; // negotiates 2026-07-28; no initialize, no Mcp-Session-Id
let tools = client.list_tools().await?;
let result = client.call_tool("add", serde_json::json!({"a": 1, "b": 2})).await?;
See the mcp-client-patterns skill for the full client API and negotiation details.
Compliance Test Suite
Four compliance test modules verify MCP specification conformance:
| Module | What It Tests |
|---|---|
| JSON-RPC format | Request/response structure, error codes, jsonrpc: "2.0" |
| Capability truthfulness | Advertised capabilities match actual server behavior |
| Behavioral compliance | Lifecycle enforcement, session handshake, notification ordering |
| Tool compliance | outputSchema ↔ structuredContent consistency, parameter validation |
Run all compliance tests:
cargo test --test compliance
Run specific gates:
# Lifecycle enforcement (-32031 for pre-init access)
cargo test --test compliance test_strict_lifecycle_rejects_before_initialized
# Capability truthfulness (capabilities match support)
cargo test --test feature_tests test_tools_capability_truthfulness
cargo test --test compliance test_runtime_capability_truthfulness
See: examples/compliance-test-custom.rs for writing custom compliance assertions.
SSE Testing
2025-11-25 only — this section (including connect_sse() reconnection/Last-Event-ID replay testing) targets the standalone GET SSE listener, which is removed entirely in 2026-07-28 (GET /mcp is unconditionally 405). On 2026-07-28, real-time SSE lives on the POST response of the originating request — test it by asserting on the streamed frames of a single call_tool/streaming request, not a separate GET connection.
Test SSE streaming behavior for progress notifications and real-time events.
// turul-mcp-server v0.4
// Call tool with SSE Accept header — returns raw Response for event parsing
let response = client.call_tool_with_sse("slow_operation", json!({"input": "test"})).await?;
assert!(response.status().is_success());
// Parse SSE events from the response body
let body = response.text().await?;
for line in body.lines() {
if line.starts_with("data: ") {
let event_data: serde_json::Value = serde_json::from_str(&line[6..])?;
// Verify progress notifications, final result, etc.
}
}
SSE reconnection testing (2025-11-25 only): Use connect_sse() to open a GET stream, then verify Last-Event-ID replay by disconnecting and reconnecting with the last seen event ID. Not applicable on 2026-07-28 — there is no GET stream to reconnect to.
Test Organization
The framework uses consolidated test binaries to minimize compilation time (43 binaries vs 155 without consolidation).
Pattern: Set autotests = false in [package], then define a single [[test]] entry that imports all test modules:
# Cargo.toml
[package]
autotests = false # MUST be under [package], not between sections
[[test]]
name = "all"
path = "tests/all.rs"
// tests/all.rs — single binary, multiple modules
#[path = "test_tools.rs"]
mod test_tools;
#[path = "test_resources.rs"]
mod test_resources;
#[path = "test_prompts.rs"]
mod test_prompts;
Why: Each test binary links the entire dependency tree. Consolidating N test files into 1 binary eliminates N-1 link steps.
Doctest Strategy
Three tiers of documentation tests, balancing coverage vs speed:
| Tier | Attribute | When | Compile Time |
|---|---|---|---|
| Critical API | (none — runs by default) | Core types, builder API | < 1s per test |
| Syntax validation | no_run |
Examples that need external state (DB, network) | Compile only |
| Full integration | ignore |
Expensive setup, run explicitly with --ignored |
Seconds |
Rule: Every ```rust block in doc comments MUST compile. Use no_run for examples that need external resources, ignore for truly expensive tests. Never use ```text for Rust code — it hides compilation errors.
TestFixtures Helpers (2025-11-25 — tests/shared)
TestFixtures provides pre-built capability objects and assertion helpers:
| Method | Returns |
|---|---|
resource_capabilities() |
{"resources": {"subscribe": true, "listChanged": false}} |
tools_capabilities() |
{"tools": {"listChanged": false}} |
prompts_capabilities() |
{"prompts": {"listChanged": false}} |
verify_initialization_response(result) |
Assert valid init response — hardcodes protocolVersion: "2025-11-25"; there is no initialize response to assert on 2026-07-28 |
verify_error_response(result) |
Assert JSON-RPC error structure |
verify_resource_list_response(result) |
Assert valid resources/list response |
verify_resource_content_response(result) |
Assert valid resources/read response |
extract_tool_structured_content(result) |
Extract structuredContent from tools/call response |
extract_tool_content_text(result) |
Extract text content from tool result |
extract_tools_list(result) |
Extract tools array from tools/list response |
Common Mistakes
-
Port conflicts in parallel tests — Always use TestServerManager::start() which allocates ephemeral ports via TcpListener::bind("127.0.0.1:0"). Never hardcode ports.
-
tokio::time::interval first-tick race — interval() fires immediately on the first tick. Background cleanup tasks should use tokio::time::sleep in a loop instead, to avoid races with TTL-sensitive tests.
-
SQLite :memory: pool isolation — Each connection in a pool gets its own in-memory database. For shared test databases, use file:{uuid}?mode=memory&cache=shared.
-
Missing Accept header — Streamable HTTP requires Accept: application/json, text/event-stream, or */*. Omitting it causes request rejection.
-
Forgetting send_initialized_notification() on a 2025-11-25 test server — In strict lifecycle mode, the server rejects all requests before the notifications/initialized handshake. Always call client.send_initialized_notification() after initialize(). Not applicable to a 2026-07-28 test server — there is no handshake to complete.
-
Testing with raw JSON instead of framework APIs — Use tool.call(json!({...}), None) for unit tests and McpTestClient for E2E. Avoid manually constructing JSON-RPC request objects.
Auth Coverage Limits
Port conflicts in parallel tests — Always use TestServerManager::start() which allocates ephemeral ports via TcpListener::bind("127.0.0.1:0"). Never hardcode ports.
tokio::time::interval first-tick race — interval() fires immediately on the first tick. Background cleanup tasks should use tokio::time::sleep in a loop instead, to avoid races with TTL-sensitive tests.
SQLite :memory: pool isolation — Each connection in a pool gets its own in-memory database. For shared test databases, use file:{uuid}?mode=memory&cache=shared.
Missing Accept header — Streamable HTTP requires Accept: application/json, text/event-stream, or */*. Omitting it causes request rejection.
Forgetting send_initialized_notification() on a 2025-11-25 test server — In strict lifecycle mode, the server rejects all requests before the notifications/initialized handshake. Always call client.send_initialized_notification() after initialize(). Not applicable to a 2026-07-28 test server — there is no handshake to complete.
Testing with raw JSON instead of framework APIs — Use tool.call(json!({...}), None) for unit tests and McpTestClient for E2E. Avoid manually constructing JSON-RPC request objects.
The compliance modules described above cover JSON-RPC format, lifecycle handshake, capability advertisement, and tool behavior. They do not currently cover:
- OAuth 2.1 authorization flows (401 challenges, token validation)
- Protected Resource Metadata discovery (RFC 9728)
- Bearer token rejection / WWW-Authenticate header correctness
- Audience/scope validation behavior
To test auth behavior, use the E2E testing pattern (McpTestClient) with OAuthResourceMiddleware registered on the server, and verify 401 responses for missing/invalid tokens manually. See the auth-patterns skill for middleware setup and the authorization-server-patterns skill for building a demo AS to test against.
Beyond This Skill
Error handling in tests? → See the error-handling-patterns skill for McpError variants and assertion patterns.
Middleware testing? → See the middleware-patterns skill for McpMiddleware trait and integration with auth/rate-limit middleware.
Task lifecycle testing? → See the task-patterns skill for the 2026-07-28 Tasks extension and the frozen 2025-11-25 in-core TaskRuntime.
Lambda testing? → See the lambda-deployment skill for local Lambda testing and DynamoDB test setup.
Auth testing? → See the auth-patterns skill for OAuthResourceMiddleware setup and the authorization-server-patterns skill for a demo AS to test against.
Server configuration? Use McpServer::builder(). See: CLAUDE.md — Basic Server