TradingAgents: Multi-Agents LLM Financial Trading Framework
test: lazy-load LLM provider clients and add API-key fixtures so the test suite runs cleanly without credentials (#588)
Yijia-Xiaof85f5d9f5d92
diff --git a/pyproject.toml b/pyproject.tomlindex 98385e32e..110611630 100644--- a/pyproject.toml+++ b/pyproject.toml@@ -40,3 +40,15 @@ include = ["tradingagents*", "cli*"][tool.setuptools.package-data]cli = ["static/*"]++[tool.pytest.ini_options]+testpaths = ["tests"]+addopts = "-ra --strict-markers"+markers = [+ "unit: fast isolated unit tests",+ "integration: tests requiring external services",+ "smoke: quick sanity-check tests",+]+filterwarnings = [+ "ignore::DeprecationWarning",+]diff --git a/tests/conftest.py b/tests/conftest.pynew file mode 100644index 000000000..504ffb12d--- /dev/null+++ b/tests/conftest.py@@ -0,0 +1,42 @@+"""Shared pytest fixtures that prevent CI hangs when API keys are absent."""++import os+from unittest.mock import MagicMock, patch++import pytest+++def pytest_configure(config):+ for marker in ("unit", "integration", "smoke"):+ config.addinivalue_line("markers", f"{marker}: {marker}-level tests")+++_API_KEY_ENV_VARS = (+ "OPENAI_API_KEY",+ "GOOGLE_API_KEY",+ "ANTHROPIC_API_KEY",+ "XAI_API_KEY",+ "DEEPSEEK_API_KEY",+ "DASHSCOPE_API_KEY",+ "ZHIPU_API_KEY",+ "OPENROUTER_API_KEY",+ "AZURE_OPENAI_API_KEY",+ "ALPHA_VANTAGE_API_KEY",+)++[email protected](autouse=True)+def _dummy_api_keys(monkeypatch):+ for env_var in _API_KEY_ENV_VARS:+ monkeypatch.setenv(env_var, os.environ.get(env_var, "placeholder"))+++def mock_llm_client():+ client = MagicMock()+ client.get_llm.return_value = MagicMock()+ with patch(+ "tradingagents.llm_clients.factory.create_llm_client",+ return_value=client,+ ):+ yield clientdiff --git a/tests/test_google_api_key.py b/tests/test_google_api_key.pyindex e1607c49a..53376ab10 100644--- a/tests/test_google_api_key.py+++ b/tests/test_google_api_key.py@@ -1,9 +1,12 @@import unittestfrom unittest.mock import patch+import pytest+from tradingagents.llm_clients.google_client import GoogleClientclass TestGoogleApiKeyStandardization(unittest.TestCase):"""Verify GoogleClient accepts unified api_key parameter."""diff --git a/tests/test_model_validation.py b/tests/test_model_validation.pyindex 50f263182..5392d7cd9 100644--- a/tests/test_model_validation.py+++ b/tests/test_model_validation.py@@ -1,6 +1,8 @@import unittestimport warnings+import pytest+from tradingagents.llm_clients.base_client import BaseLLMClientfrom tradingagents.llm_clients.model_catalog import get_known_modelsfrom tradingagents.llm_clients.validators import validate_model@@ -19,6 +21,7 @@ class DummyLLMClient(BaseLLMClient):return validate_model(self.provider, self.model)class ModelValidationTests(unittest.TestCase):def test_cli_catalog_models_are_all_validator_approved(self):for provider, models in get_known_models().items():diff --git a/tests/test_ticker_symbol_handling.py b/tests/test_ticker_symbol_handling.pyindex 858d26cd5..7fbe5315d 100644--- a/tests/test_ticker_symbol_handling.py+++ b/tests/test_ticker_symbol_handling.py@@ -1,9 +1,12 @@import unittest+import pytest+from cli.utils import normalize_ticker_symbolfrom tradingagents.agents.utils.agent_utils import build_instrument_contextclass TickerSymbolHandlingTests(unittest.TestCase):def test_normalize_ticker_symbol_preserves_exchange_suffix(self):self.assertEqual(normalize_ticker_symbol(" cnc.to "), "CNC.TO")diff --git a/tradingagents/llm_clients/factory.py b/tradingagents/llm_clients/factory.pyindex a9a7e83d8..e1d24557e 100644--- a/tradingagents/llm_clients/factory.py+++ b/tradingagents/llm_clients/factory.py@@ -1,10 +1,6 @@from typing import Optionalfrom .base_client import BaseLLMClient-from .openai_client import OpenAIClient-from .anthropic_client import AnthropicClient-from .google_client import GoogleClient-from .azure_client import AzureOpenAIClient# Providers that use the OpenAI-compatible chat completions API_OPENAI_COMPATIBLE = (@@ -20,6 +16,10 @@ def create_llm_client() -> BaseLLMClient:"""Create an LLM client for the specified provider.+ Provider modules are imported lazily so that simply importing this+ factory (e.g. during test collection) does not pull in heavy LLM SDKs+ or fail when their API keys are absent.+Args:provider: LLM provider namemodel: Model name/identifier@@ -35,15 +35,19 @@ def create_llm_client(provider_lower = provider.lower()if provider_lower in _OPENAI_COMPATIBLE:+ from .openai_client import OpenAIClientreturn OpenAIClient(model, base_url, provider=provider_lower, **kwargs)if provider_lower == "anthropic":+ from .anthropic_client import AnthropicClientreturn AnthropicClient(model, base_url, **kwargs)if provider_lower == "google":+ from .google_client import GoogleClientreturn GoogleClient(model, base_url, **kwargs)if provider_lower == "azure":+ from .azure_client import AzureOpenAIClientreturn AzureOpenAIClient(model, base_url, **kwargs)raise ValueError(f"Unsupported LLM provider: {provider}")