fix(test): add cleanup fixture and no_parallel mark for MCP tests#21284
fix(test): add cleanup fixture and no_parallel mark for MCP tests#21284
Conversation
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 <noreply@anthropic.com>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Greptile SummaryThis PR adds test isolation improvements for MCP server tests that were failing under pytest-xdist parallel execution. The changes consist of:
The cleanup fixture is the substantive fix — it ensures proper isolation regardless of execution order. The Confidence Score: 4/5
|
| Filename | Overview |
|---|---|
| tests/test_litellm/proxy/_experimental/mcp_server/test_mcp_server.py | Adds autouse cleanup fixture for global_mcp_server_manager.registry and @pytest.mark.no_parallel on two tests to improve test isolation with pytest-xdist. The cleanup fixture is well-structured with proper try/except for ImportError. Minor observation: the no_parallel mark is not enforced by any conftest or plugin - the real fix is the cleanup fixture. |
Flowchart
flowchart TD
A[Test starts] --> B[cleanup_mcp_global_state fixture: clear registry]
B --> C{Test has no_parallel mark?}
C -->|Yes| D[Run sequentially in xdist]
C -->|No| E[May run in parallel with xdist]
D --> F[Test executes with clean global state]
E --> F
F --> G[Test manipulates global_mcp_server_manager.registry]
G --> H[Test assertions]
H --> I[cleanup_mcp_global_state teardown: clear registry]
I --> J[Test ends - clean state for next test]
Last reviewed commit: cc2dff0
…ver.py Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
Summary
Fixes two MCP server test failures when running with pytest-xdist parallel execution (
--dist=loadscope):test_mcp_routing_with_conflicting_alias_and_group_nametest_oauth2_headers_passed_to_mcp_clientProblem
Both tests were failing with:
Root cause: These tests rely on
global_mcp_server_managersingleton state and complex async mocking patterns. When running with parallel execution:Solution
Two-pronged approach:
1. Added autouse fixture for state cleanup
This ensures proper isolation even when tests run sequentially in the same worker.
2. Added @pytest.mark.no_parallel
Marked these specific tests to run sequentially rather than in parallel:
This prevents the parallel execution issues while still allowing other tests in the file to benefit from parallelization.
Testing
pytest tests/test_litellm/proxy/_experimental/mcp_server/test_mcp_server.py -vRelated
--dist=loadscope🤖 Generated with Claude Code