Skip to content

Commit 74a470c

Browse files
add fixture to mock API keys in unit tests that use AnyLLM.create internally
1 parent daa8b42 commit 74a470c

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

tests/unit/conftest.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import os
2+
from collections.abc import Generator
3+
4+
import pytest
5+
6+
from any_agent.config import AgentFramework
7+
8+
9+
@pytest.fixture(autouse=True)
10+
def mock_api_keys_for_unit_tests(
11+
request: pytest.FixtureRequest,
12+
) -> Generator[None, None, None]:
13+
"""Automatically provide dummy API keys for unit tests to avoid API key requirements."""
14+
if "agent_framework" in request.fixturenames:
15+
agent_framework = request.getfixturevalue("agent_framework")
16+
# Only set dummy API key if we're in a test that uses the agent_framework fixture
17+
# and the framework is OPENAI (which uses any-llm with the AnyLLM.create class-based interface)
18+
if agent_framework == AgentFramework.OPENAI:
19+
os.environ["MISTRAL_API_KEY"] = "dummy-mistral-key-for-unit-tests"
20+
yield

0 commit comments

Comments
 (0)