Skip to content

Commit b26aecb

Browse files
authored
Update integ tests to isolate provider-based tests (#396)
Move tests to `tests_integ` as `tests-integ` is not a proper module name. Also extract all provider ignoring to a new providers file which centralizes the environment variables needed.
1 parent ae01d57 commit b26aecb

26 files changed

+84
-49
lines changed

.github/workflows/integration-test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,4 @@ jobs:
6969
AWS_REGION_NAME: us-east-1 # Needed for LiteLLM
7070
id: tests
7171
run: |
72-
hatch test tests-integ
72+
hatch test tests_integ

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ test = [
195195
"hatch test --cover --cov-report html --cov-report xml {args}"
196196
]
197197
test-integ = [
198-
"hatch test tests-integ {args}"
198+
"hatch test tests_integ {args}"
199199
]
200200
prepare = [
201201
"hatch fmt --linter",
@@ -230,7 +230,7 @@ ignore_missing_imports = true
230230

231231
[tool.ruff]
232232
line-length = 120
233-
include = ["examples/**/*.py", "src/**/*.py", "tests/**/*.py", "tests-integ/**/*.py"]
233+
include = ["examples/**/*.py", "src/**/*.py", "tests/**/*.py", "tests_integ/**/*.py"]
234234

235235
[tool.ruff.lint]
236236
select = [
File renamed without changes.
File renamed without changes.
File renamed without changes.

tests_integ/models/__init__.py

Whitespace-only changes.

tests_integ/models/providers.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import os
2+
from dataclasses import dataclass
3+
4+
import requests
5+
from pytest import mark
6+
7+
8+
@dataclass
9+
class ApiKeyProviderInfo:
10+
"""Provider-based info for providers that require an APIKey via environment variables."""
11+
12+
def __init__(self, id: str, environment_variable: str) -> None:
13+
self.id = id
14+
self.environment_variable = environment_variable
15+
self.mark = mark.skipif(
16+
self.environment_variable not in os.environ,
17+
reason=f"{self.environment_variable} environment variable missing",
18+
)
19+
20+
21+
class OllamaProviderInfo:
22+
"""Special case ollama as it's dependent on the server being available."""
23+
24+
def __init__(self):
25+
self.id = "ollama"
26+
27+
is_server_available = False
28+
try:
29+
is_server_available = requests.get("http://localhost:11434").ok
30+
except requests.exceptions.ConnectionError:
31+
pass
32+
33+
self.mark = mark.skipif(
34+
not is_server_available,
35+
reason="Local Ollama endpoint not available at localhost:11434",
36+
)
37+
38+
39+
anthropic = ApiKeyProviderInfo(id="anthropic", environment_variable="ANTHROPIC_API_KEY")
40+
cohere = ApiKeyProviderInfo(id="cohere", environment_variable="CO_API_KEY")
41+
llama = ApiKeyProviderInfo(id="cohere", environment_variable="LLAMA_API_KEY")
42+
mistral = ApiKeyProviderInfo(id="mistral", environment_variable="MISTRAL_API_KEY")
43+
openai = ApiKeyProviderInfo(id="openai", environment_variable="OPENAI_API_KEY")
44+
writer = ApiKeyProviderInfo(id="writer", environment_variable="WRITER_API_KEY")
45+
46+
ollama = OllamaProviderInfo()

tests-integ/test_model_anthropic.py renamed to tests_integ/models/test_model_anthropic.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
import strands
77
from strands import Agent
88
from strands.models.anthropic import AnthropicModel
9+
from tests_integ.models import providers
10+
11+
# these tests only run if we have the anthropic api key
12+
pytestmark = providers.anthropic.mark
913

1014

1115
@pytest.fixture(scope="module")
@@ -53,15 +57,13 @@ class Weather(BaseModel):
5357
return Weather(time="12:00", weather="sunny")
5458

5559

56-
@pytest.mark.skipif("ANTHROPIC_API_KEY" not in os.environ, reason="ANTHROPIC_API_KEY environment variable missing")
5760
def test_agent_invoke(agent):
5861
result = agent("What is the time and weather in New York?")
5962
text = result.message["content"][0]["text"].lower()
6063

6164
assert all(string in text for string in ["12:00", "sunny"])
6265

6366

64-
@pytest.mark.skipif("ANTHROPIC_API_KEY" not in os.environ, reason="ANTHROPIC_API_KEY environment variable missing")
6567
@pytest.mark.asyncio
6668
async def test_agent_invoke_async(agent):
6769
result = await agent.invoke_async("What is the time and weather in New York?")
@@ -70,7 +72,6 @@ async def test_agent_invoke_async(agent):
7072
assert all(string in text for string in ["12:00", "sunny"])
7173

7274

73-
@pytest.mark.skipif("ANTHROPIC_API_KEY" not in os.environ, reason="ANTHROPIC_API_KEY environment variable missing")
7475
@pytest.mark.asyncio
7576
async def test_agent_stream_async(agent):
7677
stream = agent.stream_async("What is the time and weather in New York?")
@@ -83,14 +84,12 @@ async def test_agent_stream_async(agent):
8384
assert all(string in text for string in ["12:00", "sunny"])
8485

8586

86-
@pytest.mark.skipif("ANTHROPIC_API_KEY" not in os.environ, reason="ANTHROPIC_API_KEY environment variable missing")
8787
def test_structured_output(agent, weather):
8888
tru_weather = agent.structured_output(type(weather), "The time is 12:00 and the weather is sunny")
8989
exp_weather = weather
9090
assert tru_weather == exp_weather
9191

9292

93-
@pytest.mark.skipif("ANTHROPIC_API_KEY" not in os.environ, reason="ANTHROPIC_API_KEY environment variable missing")
9493
@pytest.mark.asyncio
9594
async def test_agent_structured_output_async(agent, weather):
9695
tru_weather = await agent.structured_output_async(type(weather), "The time is 12:00 and the weather is sunny")
File renamed without changes.

tests-integ/test_model_cohere.py renamed to tests_integ/models/test_model_cohere.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
import strands
66
from strands import Agent
77
from strands.models.openai import OpenAIModel
8+
from tests_integ.models import providers
9+
10+
# these tests only run if we have the cohere api key
11+
pytestmark = providers.cohere.mark
812

913

1014
@pytest.fixture
@@ -37,10 +41,6 @@ def agent(model, tools):
3741
return Agent(model=model, tools=tools)
3842

3943

40-
@pytest.mark.skipif(
41-
"CO_API_KEY" not in os.environ,
42-
reason="CO_API_KEY environment variable missing",
43-
)
4444
def test_agent(agent):
4545
result = agent("What is the time and weather in New York?")
4646
text = result.message["content"][0]["text"].lower()

0 commit comments

Comments
 (0)