1414
1515import asyncio
1616import logging
17- import os
18- import sys
1917import time
20- import types as ptypes
2118from unittest .mock import MagicMock
2219from unittest .mock import patch
2320
2421from fastapi .testclient import TestClient
2522from google .adk .agents .base_agent import BaseAgent
2623from google .adk .agents .run_config import RunConfig
2724from google .adk .cli .fast_api import get_fast_api_app
28- from google .adk .cli .utils import envs
2925from google .adk .events import Event
3026from google .adk .runners import Runner
3127from google .adk .sessions .base_session_service import ListSessionsResponse
@@ -48,22 +44,7 @@ def __init__(self, name):
4844 self .sub_agents = []
4945
5046
51- # Set up dummy module and add to sys.modules
52- dummy_module = ptypes .ModuleType ("test_agent" )
53- dummy_module .agent = ptypes .SimpleNamespace (
54- root_agent = DummyAgent (name = "dummy_agent" )
55- )
56- sys .modules ["test_app" ] = dummy_module
57-
58- # Try to load environment variables, with a fallback for testing
59- try :
60- envs .load_dotenv_for_agent ("test_app" , "." )
61- except Exception as e :
62- logger .warning (f"Could not load environment variables: { e } " )
63- # Create a basic .env file if needed
64- if not os .path .exists (".env" ):
65- with open (".env" , "w" ) as f :
66- f .write ("# Test environment variables\n " )
47+ root_agent = DummyAgent (name = "dummy_agent" )
6748
6849
6950# Create sample events that our mocked runner will return
@@ -150,6 +131,20 @@ def test_session_info():
150131 }
151132
152133
134+ @pytest .fixture
135+ def mock_agent_loader ():
136+
137+ class MockAgentLoader :
138+
139+ def __init__ (self , agents_dir : str ):
140+ pass
141+
142+ def load_agent (self , app_name ):
143+ return root_agent
144+
145+ return MockAgentLoader ("." )
146+
147+
153148@pytest .fixture
154149def mock_session_service ():
155150 """Create a mock session service that uses an in-memory dictionary."""
@@ -287,24 +282,33 @@ def mock_memory_service():
287282
288283
289284@pytest .fixture
290- def test_app (mock_session_service , mock_artifact_service , mock_memory_service ):
285+ def test_app (
286+ mock_session_service ,
287+ mock_artifact_service ,
288+ mock_memory_service ,
289+ mock_agent_loader ,
290+ ):
291291 """Create a TestClient for the FastAPI app without starting a server."""
292292
293293 # Patch multiple services and signal handlers
294294 with (
295295 patch ("signal.signal" , return_value = None ),
296296 patch (
297- "google.adk.cli.fast_api.InMemorySessionService" , # Changed this line
297+ "google.adk.cli.fast_api.InMemorySessionService" ,
298298 return_value = mock_session_service ,
299299 ),
300300 patch (
301- "google.adk.cli.fast_api.InMemoryArtifactService" , # Make consistent
301+ "google.adk.cli.fast_api.InMemoryArtifactService" ,
302302 return_value = mock_artifact_service ,
303303 ),
304304 patch (
305- "google.adk.cli.fast_api.InMemoryMemoryService" , # Make consistent
305+ "google.adk.cli.fast_api.InMemoryMemoryService" ,
306306 return_value = mock_memory_service ,
307307 ),
308+ patch (
309+ "google.adk.cli.fast_api.AgentLoader" ,
310+ return_value = mock_agent_loader ,
311+ ),
308312 ):
309313 # Get the FastAPI app, but don't actually run it
310314 app = get_fast_api_app (
0 commit comments