Fixture that creates a temporary user agent module for testing. Yields a callable that creates an agent module with the given name and returns the loaded agent.
(tmp_path, monkeypatch)
| 1536 | |
| 1537 | @pytest.fixture |
| 1538 | def user_agent_module(tmp_path, monkeypatch): |
| 1539 | """Fixture that creates a temporary user agent module for testing. |
| 1540 | |
| 1541 | Yields a callable that creates an agent module with the given name and |
| 1542 | returns the loaded agent. |
| 1543 | """ |
| 1544 | created_modules = [] |
| 1545 | original_path = None |
| 1546 | |
| 1547 | def _create_agent(agent_dir_name: str): |
| 1548 | nonlocal original_path |
| 1549 | agent_dir = tmp_path / "agents" / agent_dir_name |
| 1550 | agent_dir.mkdir(parents=True, exist_ok=True) |
| 1551 | (tmp_path / "agents" / "__init__.py").write_text("", encoding="utf-8") |
| 1552 | (agent_dir / "__init__.py").write_text("", encoding="utf-8") |
| 1553 | |
| 1554 | agent_source = f"""\ |
| 1555 | from google.adk.agents.llm_agent import LlmAgent |
| 1556 | |
| 1557 | class MyAgent(LlmAgent): |
| 1558 | pass |
| 1559 | |
| 1560 | root_agent = MyAgent(name="{agent_dir_name}", model="gemini-2.5-flash") |
| 1561 | """ |
| 1562 | (agent_dir / "agent.py").write_text(agent_source, encoding="utf-8") |
| 1563 | |
| 1564 | monkeypatch.chdir(tmp_path) |
| 1565 | if original_path is None: |
| 1566 | original_path = str(tmp_path) |
| 1567 | sys.path.insert(0, original_path) |
| 1568 | |
| 1569 | module_name = f"agents.{agent_dir_name}.agent" |
| 1570 | module = importlib.import_module(module_name) |
| 1571 | created_modules.append(module_name) |
| 1572 | return module.root_agent |
| 1573 | |
| 1574 | yield _create_agent |
| 1575 | |
| 1576 | # Cleanup |
| 1577 | if original_path and original_path in sys.path: |
| 1578 | sys.path.remove(original_path) |
| 1579 | for mod_name in list(sys.modules.keys()): |
| 1580 | if mod_name.startswith("agents"): |
| 1581 | del sys.modules[mod_name] |
| 1582 | |
| 1583 | |
| 1584 | class TestRunnerInferAgentOrigin: |
no test coverage detected