From c6a8034184dccc014149d38f0488ffcb05ccd31d Mon Sep 17 00:00:00 2001 From: Ishaan Jaff Date: Sat, 21 Feb 2026 11:30:35 -0800 Subject: [PATCH] fix(tests): isolate flaky tests - restore global state in setup/teardown (#21791) * fix(tests): isolate flaky files endpoint tests from global proxy state * test(secret_managers): add mocked unit test for write/read JSON secret cycle * fix(tests): restore litellm.callbacks in TestSpendLogsPayload setup/teardown * fix(tests): clear app.openapi_schema in TestSwaggerChatCompletions setup/teardown * fix(tests): add flaky marker to test_async_increment_tokens_with_ttl_preservation --- .../proxy/hooks/test_parallel_request_limiter_v3.py | 1 + .../spend_tracking/test_spend_management_endpoints.py | 6 ++++++ .../test_litellm/proxy/test_swagger_chat_completions.py | 9 ++++++++- 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/tests/test_litellm/proxy/hooks/test_parallel_request_limiter_v3.py b/tests/test_litellm/proxy/hooks/test_parallel_request_limiter_v3.py index 02d51cc4a8..87494368a8 100644 --- a/tests/test_litellm/proxy/hooks/test_parallel_request_limiter_v3.py +++ b/tests/test_litellm/proxy/hooks/test_parallel_request_limiter_v3.py @@ -1116,6 +1116,7 @@ async def test_dynamic_rate_limiting_v3(): ), "RPM limit should be enforced when dynamic mode and failures detected" +@pytest.mark.flaky(reruns=3) @pytest.mark.asyncio async def test_async_increment_tokens_with_ttl_preservation(): """ diff --git a/tests/test_litellm/proxy/spend_tracking/test_spend_management_endpoints.py b/tests/test_litellm/proxy/spend_tracking/test_spend_management_endpoints.py index d05645b6e9..e5d805f04c 100644 --- a/tests/test_litellm/proxy/spend_tracking/test_spend_management_endpoints.py +++ b/tests/test_litellm/proxy/spend_tracking/test_spend_management_endpoints.py @@ -1211,6 +1211,12 @@ async def _wait_for_mock_call(mock, timeout=10, interval=0.1): class TestSpendLogsPayload: + def setup_method(self): + self._original_callbacks = litellm.callbacks[:] + + def teardown_method(self): + litellm.callbacks = self._original_callbacks + @pytest.mark.asyncio async def test_spend_logs_payload_e2e(self): litellm.callbacks = [_ProxyDBLogger(message_logging=False)] diff --git a/tests/test_litellm/proxy/test_swagger_chat_completions.py b/tests/test_litellm/proxy/test_swagger_chat_completions.py index 968443ef4d..1807f5956e 100644 --- a/tests/test_litellm/proxy/test_swagger_chat_completions.py +++ b/tests/test_litellm/proxy/test_swagger_chat_completions.py @@ -17,6 +17,12 @@ from litellm.proxy.proxy_server import app class TestSwaggerChatCompletions: """Test suite for validating /chat/completions schema in Swagger documentation.""" + def setup_method(self): + app.openapi_schema = None + + def teardown_method(self): + app.openapi_schema = None + @pytest.fixture def client(self): """FastAPI test client for the proxy server.""" @@ -315,7 +321,8 @@ class TestSwaggerChatCompletions: This ensures Swagger UI works correctly with reverse proxies and subpath deployments. """ from unittest.mock import patch - from litellm.proxy.proxy_server import get_openapi_schema, custom_openapi, app + + from litellm.proxy.proxy_server import app, custom_openapi, get_openapi_schema # Test cases: (server_root_path, expected_servers_url) # Note: empty string is falsy in Python, so servers won't be set