From cc2dff058154af166d51e3cdeb68c1ab57db8f08 Mon Sep 17 00:00:00 2001 From: Julio Quinteros Pro Date: Sun, 15 Feb 2026 20:42:03 -0300 Subject: [PATCH] fix(test): add cleanup fixture and no_parallel mark for MCP tests Two MCP server tests were failing when run with pytest-xdist parallel execution (--dist=loadscope): - test_mcp_routing_with_conflicting_alias_and_group_name - test_oauth2_headers_passed_to_mcp_client Both tests showed assertion failures where mocks weren't being called (0 times instead of expected 1 time). Root cause: These tests rely on global_mcp_server_manager singleton state and complex async mocking that doesn't work reliably with parallel execution. Each worker process can have different state and patches may not apply correctly. Solution: 1. Added autouse fixture to clean up global_mcp_server_manager registry before and after each test for better isolation 2. Added @pytest.mark.no_parallel to these specific tests to ensure they run sequentially, avoiding parallel execution issues This approach maintains test reliability while allowing other tests in the file to still benefit from parallelization. Fixes test failures exposed by PR #21277. Co-Authored-By: Claude Sonnet 4.5 --- .../mcp_server/test_mcp_server.py | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/tests/test_litellm/proxy/_experimental/mcp_server/test_mcp_server.py b/tests/test_litellm/proxy/_experimental/mcp_server/test_mcp_server.py index 07c3dfcc76..30a917bb22 100644 --- a/tests/test_litellm/proxy/_experimental/mcp_server/test_mcp_server.py +++ b/tests/test_litellm/proxy/_experimental/mcp_server/test_mcp_server.py @@ -16,6 +16,28 @@ from litellm.proxy._types import ( from litellm.types.mcp_server.mcp_server_manager import MCPServer +@pytest.fixture(autouse=True) +def cleanup_mcp_global_state(): + """Clean up MCP global state before and after each test. + + This fixture ensures test isolation when running with pytest-xdist + parallel execution. Without this, global_mcp_server_manager state + can leak between tests causing mock assertion failures. + """ + try: + from litellm.proxy._experimental.mcp_server.mcp_server_manager import ( + global_mcp_server_manager, + ) + # Clear before test + global_mcp_server_manager.registry.clear() + yield + # Clear after test + global_mcp_server_manager.registry.clear() + except ImportError: + # MCP not available, skip cleanup + yield + + @pytest.mark.asyncio async def test_mcp_server_tool_call_body_contains_request_data(): """Test that proxy_server_request body contains name and arguments""" @@ -756,6 +778,7 @@ async def test_concurrent_initialize_session_managers(): @pytest.mark.asyncio +@pytest.mark.no_parallel async def test_mcp_routing_with_conflicting_alias_and_group_name(): """ Tests (GH #14536) where an MCP server alias (e.g., "group/id") @@ -839,6 +862,7 @@ async def test_mcp_routing_with_conflicting_alias_and_group_name(): @pytest.mark.asyncio +@pytest.mark.no_parallel async def test_oauth2_headers_passed_to_mcp_client(): """Test that OAuth2 headers are properly passed through to the MCP client for OAuth2 servers like github_mcp""" try: