88from strands .models .anthropic import AnthropicModel
99
1010
11- @pytest .fixture
11+ @pytest .fixture ( scope = "module" )
1212def model ():
1313 return AnthropicModel (
1414 client_args = {
@@ -19,7 +19,7 @@ def model():
1919 )
2020
2121
22- @pytest .fixture
22+ @pytest .fixture ( scope = "module" )
2323def tools ():
2424 @strands .tool
2525 def tool_time () -> str :
@@ -32,32 +32,67 @@ def tool_weather() -> str:
3232 return [tool_time , tool_weather ]
3333
3434
35- @pytest .fixture
35+ @pytest .fixture ( scope = "module" )
3636def system_prompt ():
3737 return "You are an AI assistant."
3838
3939
40- @pytest .fixture
40+ @pytest .fixture ( scope = "module" )
4141def agent (model , tools , system_prompt ):
4242 return Agent (model = model , tools = tools , system_prompt = system_prompt )
4343
4444
45+ @pytest .fixture (scope = "module" )
46+ def weather ():
47+ class Weather (BaseModel ):
48+ """Extracts the time and weather from the user's message with the exact strings."""
49+
50+ time : str
51+ weather : str
52+
53+ return Weather (time = "12:00" , weather = "sunny" )
54+
55+
4556@pytest .mark .skipif ("ANTHROPIC_API_KEY" not in os .environ , reason = "ANTHROPIC_API_KEY environment variable missing" )
46- def test_agent (agent ):
57+ def test_agent_invoke (agent ):
4758 result = agent ("What is the time and weather in New York?" )
4859 text = result .message ["content" ][0 ]["text" ].lower ()
4960
5061 assert all (string in text for string in ["12:00" , "sunny" ])
5162
5263
5364@pytest .mark .skipif ("ANTHROPIC_API_KEY" not in os .environ , reason = "ANTHROPIC_API_KEY environment variable missing" )
54- def test_structured_output (model ):
55- class Weather (BaseModel ):
56- time : str
57- weather : str
65+ @pytest .mark .asyncio
66+ async def test_agent_invoke_async (agent ):
67+ result = await agent .invoke_async ("What is the time and weather in New York?" )
68+ text = result .message ["content" ][0 ]["text" ].lower ()
69+
70+ assert all (string in text for string in ["12:00" , "sunny" ])
5871
59- agent = Agent (model = model )
60- result = agent .structured_output (Weather , "The time is 12:00 and the weather is sunny" )
61- assert isinstance (result , Weather )
62- assert result .time == "12:00"
63- assert result .weather == "sunny"
72+
73+ @pytest .mark .skipif ("ANTHROPIC_API_KEY" not in os .environ , reason = "ANTHROPIC_API_KEY environment variable missing" )
74+ @pytest .mark .asyncio
75+ async def test_agent_stream_async (agent ):
76+ stream = agent .stream_async ("What is the time and weather in New York?" )
77+ async for event in stream :
78+ _ = event
79+
80+ result = event ["result" ]
81+ text = result .message ["content" ][0 ]["text" ].lower ()
82+
83+ assert all (string in text for string in ["12:00" , "sunny" ])
84+
85+
86+ @pytest .mark .skipif ("ANTHROPIC_API_KEY" not in os .environ , reason = "ANTHROPIC_API_KEY environment variable missing" )
87+ def test_structured_output (agent , weather ):
88+ tru_weather = agent .structured_output (type (weather ), "The time is 12:00 and the weather is sunny" )
89+ exp_weather = weather
90+ assert tru_weather == exp_weather
91+
92+
93+ @pytest .mark .skipif ("ANTHROPIC_API_KEY" not in os .environ , reason = "ANTHROPIC_API_KEY environment variable missing" )
94+ @pytest .mark .asyncio
95+ async def test_agent_structured_output_async (agent , weather ):
96+ tru_weather = await agent .structured_output_async (type (weather ), "The time is 12:00 and the weather is sunny" )
97+ exp_weather = weather
98+ assert tru_weather == exp_weather
0 commit comments