Skip to content

Commit 5573271

Browse files
Varun SharmaCopilot
andcommitted
fix: use AsyncMock to fix coverage tracking in CI
Replace inner async function with patch.object + AsyncMock to avoid coverage blind spot when the mock runs inside the server's task context with pytest-xdist parallel workers. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 8c3981e commit 5573271

1 file changed

Lines changed: 9 additions & 13 deletions

File tree

tests/server/mcpserver/test_server.py

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -307,20 +307,16 @@ async def test_handle_call_tool_defensive_exception_handler(self):
307307
mcp = MCPServer()
308308
mcp.add_tool(tool_fn)
309309

310-
original_call_tool = mcp.call_tool
311-
312-
async def patched_call_tool(name: str, arguments: dict[str, Any]) -> Any:
313-
raise RuntimeError("internal db connection string leaked")
314-
315310
async with Client(mcp) as client:
316-
mcp.call_tool = patched_call_tool # type: ignore[assignment]
317-
result = await client.call_tool("tool_fn", {"x": 1, "y": 2})
318-
mcp.call_tool = original_call_tool # type: ignore[assignment]
319-
assert result.is_error is True
320-
content = result.content[0]
321-
assert isinstance(content, TextContent)
322-
assert "unexpected error" in content.text.lower()
323-
assert "internal db connection string leaked" not in content.text
311+
with patch.object(
312+
mcp, "call_tool", new_callable=AsyncMock, side_effect=RuntimeError("internal db leak")
313+
):
314+
result = await client.call_tool("tool_fn", {"x": 1, "y": 2})
315+
assert result.is_error is True
316+
content = result.content[0]
317+
assert isinstance(content, TextContent)
318+
assert "unexpected error" in content.text.lower()
319+
assert "internal db leak" not in content.text
324320

325321
async def test_tool_return_value_conversion(self):
326322
mcp = MCPServer()

0 commit comments

Comments
 (0)