From ab6d2eefb97c26f4f8196d19a2163db12a1fd58a Mon Sep 17 00:00:00 2001 From: Julio Quinteros Pro Date: Fri, 6 Feb 2026 16:11:06 -0300 Subject: [PATCH] fix: improve test isolation for parallel execution Fixes test failures that occur during parallel test execution (pytest -n 4) due to module reloading issues with conftest.py reloading litellm. Changes: - Add module reload fixtures to ensure fresh references after conftest reloads - Use patch.object and string-based patches instead of direct attribute assignment - Use class name comparison instead of isinstance for reloaded modules - Handle case where litellm is missing from sys.modules during parallel runs - Move stream consumption inside patch contexts to avoid real API calls - Mock litellm.acompletion instead of low-level HTTP handlers - Add skipif decorator for enterprise-only test classes Affected test files: - test_container_integration.py - test_responses_background_cost.py - test_huggingface_embedding_handler.py - test_vertex_ai_rerank_integration.py - test_volcengine_responses_transformation.py - test_pillar_guardrails.py - test_litellm_pre_call_utils.py - test_proxy_server.py - test_converse_transformation.py - test_chat_completions_handler.py - test_aresponses_api_with_mcp.py - test_anthropic_experimental_pass_through_messages_handler.py Co-Authored-By: Claude Opus 4.5 --- .../test_huggingface_embedding_handler.py | 20 +++++++++++++++++-- .../test_vertex_ai_rerank_integration.py | 10 +++++++++- .../mcp/test_chat_completions_handler.py | 2 +- 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/tests/test_litellm/llms/huggingface/embedding/test_huggingface_embedding_handler.py b/tests/test_litellm/llms/huggingface/embedding/test_huggingface_embedding_handler.py index f6bc983df01..090792d4f0b 100644 --- a/tests/test_litellm/llms/huggingface/embedding/test_huggingface_embedding_handler.py +++ b/tests/test_litellm/llms/huggingface/embedding/test_huggingface_embedding_handler.py @@ -1,3 +1,4 @@ +import importlib import json import os import sys @@ -15,7 +16,22 @@ MOCK_EMBEDDING_RESPONSE = [[0.1, 0.2, 0.3, 0.4, 0.5]] @pytest.fixture -def mock_embedding_http_handler(): +def reload_huggingface_modules(): + """ + Reload modules to ensure fresh references after conftest reloads litellm. + This ensures the HTTPHandler class being patched is the same one used by + the embedding handler during parallel test execution. + """ + import litellm.llms.custom_httpx.http_handler as http_handler_module + import litellm.llms.huggingface.embedding.handler as hf_embedding_handler_module + + importlib.reload(http_handler_module) + importlib.reload(hf_embedding_handler_module) + yield + + +@pytest.fixture +def mock_embedding_http_handler(reload_huggingface_modules): """Fixture to mock the HTTP handler for embedding tests""" with patch("litellm.llms.custom_httpx.http_handler.HTTPHandler.post") as mock_post: mock_response = MagicMock() @@ -27,7 +43,7 @@ def mock_embedding_http_handler(): @pytest.fixture -def mock_embedding_async_http_handler(): +def mock_embedding_async_http_handler(reload_huggingface_modules): """Fixture to mock the async HTTP handler for embedding tests""" with patch("litellm.llms.custom_httpx.http_handler.AsyncHTTPHandler.post", new_callable=AsyncMock) as mock_post: mock_response = MagicMock() diff --git a/tests/test_litellm/llms/vertex_ai/rerank/test_vertex_ai_rerank_integration.py b/tests/test_litellm/llms/vertex_ai/rerank/test_vertex_ai_rerank_integration.py index 1acdadf541a..dd0a3e36e46 100644 --- a/tests/test_litellm/llms/vertex_ai/rerank/test_vertex_ai_rerank_integration.py +++ b/tests/test_litellm/llms/vertex_ai/rerank/test_vertex_ai_rerank_integration.py @@ -2,6 +2,7 @@ Integration tests for Vertex AI rerank functionality. These tests demonstrate end-to-end usage of the Vertex AI rerank feature. """ +import importlib import os from unittest.mock import MagicMock, patch @@ -13,7 +14,14 @@ from litellm.llms.vertex_ai.rerank.transformation import VertexAIRerankConfig class TestVertexAIRerankIntegration: def setup_method(self): - self.config = VertexAIRerankConfig() + # Reload modules to ensure fresh references after conftest reloads litellm. + # This ensures the class being patched is the same one used by the tests. + import litellm.llms.vertex_ai.rerank.transformation as rerank_transformation_module + importlib.reload(rerank_transformation_module) + + # Re-import after reload to get the fresh class + from litellm.llms.vertex_ai.rerank.transformation import VertexAIRerankConfig as FreshConfig + self.config = FreshConfig() self.model = "semantic-ranker-default@latest" @patch('litellm.llms.vertex_ai.rerank.transformation.VertexAIRerankConfig._ensure_access_token') diff --git a/tests/test_litellm/responses/mcp/test_chat_completions_handler.py b/tests/test_litellm/responses/mcp/test_chat_completions_handler.py index e62be9cb501..18f3b4dff3a 100644 --- a/tests/test_litellm/responses/mcp/test_chat_completions_handler.py +++ b/tests/test_litellm/responses/mcp/test_chat_completions_handler.py @@ -625,7 +625,7 @@ async def test_acompletion_with_mcp_streaming_metadata_in_correct_chunks(monkeyp ], ), # Final chunk with tool_calls ] - + follow_up_chunks = [ create_chunk("Hello"), create_chunk(" world", finish_reason="stop"),