From 33470fe269a617769a99b5bc48d2b12b71e1a5ed Mon Sep 17 00:00:00 2001 From: Alexsander Hamir Date: Tue, 27 Jan 2026 13:06:08 -0800 Subject: [PATCH] fix(test): use autouse fixture to ensure module cleanup across all tests Added autouse fixture that reloads constants and auth_checks modules before and after each test. This ensures complete test isolation and prevents any test from polluting module state for subsequent tests, regardless of test execution order. --- .../proxy/auth/test_auth_checks.py | 26 ++++++++++++++----- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/tests/test_litellm/proxy/auth/test_auth_checks.py b/tests/test_litellm/proxy/auth/test_auth_checks.py index 174f424748f..3116e4ade96 100644 --- a/tests/test_litellm/proxy/auth/test_auth_checks.py +++ b/tests/test_litellm/proxy/auth/test_auth_checks.py @@ -44,6 +44,24 @@ def set_salt_key(monkeypatch): monkeypatch.setenv("LITELLM_SALT_KEY", "sk-1234") +@pytest.fixture(autouse=True) +def reset_constants_module(): + """Reset constants module to ensure clean state before each test""" + import importlib + from litellm import constants + from litellm.proxy.auth import auth_checks + + # Reload modules before test + importlib.reload(constants) + importlib.reload(auth_checks) + + yield + + # Reload modules after test to clean up + importlib.reload(constants) + importlib.reload(auth_checks) + + @pytest.fixture def valid_sso_user_defined_values(): return LiteLLM_UserTable( @@ -156,19 +174,13 @@ def test_get_cli_jwt_auth_token_default_expiration(valid_sso_user_defined_values def test_get_cli_jwt_auth_token_custom_expiration( - valid_sso_user_defined_values, monkeypatch, request + valid_sso_user_defined_values, monkeypatch ): """Test generating CLI JWT token with custom expiration via environment variable""" import importlib from litellm import constants from litellm.proxy.auth import auth_checks - # Register cleanup to reload modules after test (monkeypatch restores env var automatically) - def cleanup_modules(): - importlib.reload(constants) - importlib.reload(auth_checks) - request.addfinalizer(cleanup_modules) - # Set custom expiration to 48 hours monkeypatch.setenv("LITELLM_CLI_JWT_EXPIRATION_HOURS", "48")