Skip to content

Commit b0c5f7e

Browse files
committed
fix: refactor tests code
1 parent 0dd5163 commit b0c5f7e

File tree

8 files changed

+147
-125
lines changed

8 files changed

+147
-125
lines changed

pyproject.toml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,12 @@ test = [
144144
]
145145

146146
test-integ = [
147-
"hatch test tests-integ {args}"
147+
"hatch test tests_integ {args}"
148+
]
149+
150+
[tool.pytest.ini_options]
151+
testpaths = [
152+
"tests"
148153
]
149154

150155
[tool.mypy]
@@ -165,7 +170,7 @@ ignore_missing_imports = false
165170

166171
[tool.ruff]
167172
line-length = 120
168-
include = ["src/**/*.py", "tests/**/*.py", "tools/**/*.py","tests-integ/**/*.py"]
173+
include = ["src/**/*.py", "tests/**/*.py", "tools/**/*.py","tests_integ/**/*.py"]
169174

170175
[tool.ruff.lint]
171176
select = [

tests-integ/integration_test.py

Lines changed: 0 additions & 123 deletions
This file was deleted.

tests_integ/conftest.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
"""Common fixtures for integration tests."""
2+
3+
from pathlib import Path
4+
5+
import pytest
6+
7+
8+
@pytest.fixture
9+
def tmp_file_structure(tmp_path: Path):
10+
"""Creates a temporary directory structure for tool creation and returns paths."""
11+
tools_dir = tmp_path / ".strands" / "tools"
12+
tools_dir.mkdir(parents=True, exist_ok=True)
13+
return {"tools_dir": tools_dir, "root_dir": tmp_path}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
"""Test building agent from specification file using semantic creation."""
2+
3+
import sys
4+
from unittest import mock
5+
6+
from strands import Agent
7+
8+
from strands_agents_builder import strands
9+
10+
11+
def test_build_agent_from_specification_file(tmp_path):
12+
"""Test building agent from agent-spec.txt using semantic creation."""
13+
14+
# Create agent specification file
15+
spec_content = """Agent Specification:
16+
- Role: Math Tutor Agent
17+
- Purpose: Help students with basic arithmetic and algebra
18+
- Tools needed: calculator, file_read, current_time
19+
- Create specialized tools for math tutoring
20+
"""
21+
22+
spec_file = tmp_path / "agent-spec.txt"
23+
spec_file.write_text(spec_content)
24+
25+
# Simulate: cat agent-spec.txt | strands "Build a specialized agent based on these specifications"
26+
query = f"Build a specialized agent based on these specifications:\n\n{spec_content}"
27+
28+
with mock.patch.object(sys, "argv", ["strands", query]):
29+
strands.main()
30+
31+
# Validate agent
32+
agent = Agent(
33+
load_tools_from_directory=True,
34+
callback_handler=None,
35+
)
36+
37+
# Validate agent was created successfully
38+
assert agent is not None
39+
40+
# Validate agent has exactly 3 specified tools
41+
required_tools = ["calculator", "file_read", "current_time"]
42+
for tool_name in required_tools:
43+
assert hasattr(agent.tool, tool_name), f"Agent missing required tool: {tool_name}"
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from unittest import mock
2+
3+
from strands_agents_builder import strands
4+
5+
6+
@mock.patch.object(strands, "get_user_input")
7+
@mock.patch.dict("os.environ", {"STRANDS_TOOL_CONSOLE_MODE": "enabled"})
8+
def test_cli_with_custom_tool_from_load_file(mock_user_input, capsys, tmp_file_structure):
9+
"""Test loading and using a custom tool via the load_tool function in interactive mode."""
10+
11+
custom_tool_code = '''from strands import tool
12+
13+
@tool
14+
def reverse_text(text: str) -> dict:
15+
"""Reverse the input text."""
16+
reversed_text = text[::-1]
17+
return {
18+
"status": "success",
19+
"content": [{"text": reversed_text}]
20+
}
21+
'''
22+
tool_file = tmp_file_structure["tools_dir"] / "reverse_tool.py"
23+
tool_file.write_text(custom_tool_code)
24+
25+
user_inputs = [f"Load the tool from {tool_file}", "Use reverse_text on `hello,world`", "exit"]
26+
mock_user_input.side_effect = user_inputs
27+
28+
strands.main()
29+
30+
out = capsys.readouterr().out
31+
assert "olleh" in out, f"Expected 'olleh' in output, but got:\n{out}"
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import io
2+
import sys
3+
from unittest import mock
4+
5+
from strands.agent import Agent
6+
7+
from strands_agents_builder import strands
8+
9+
10+
@mock.patch.object(sys, "stdin")
11+
@mock.patch.dict("os.environ", {"STRANDS_TOOL_CONSOLE_MODE": "enabled"})
12+
def test_interactive_model_create_tool_then_validate(mock_stdin, capsys, tmp_file_structure):
13+
"""
14+
Test creating a calculator tool via CLI and validating its functionality.
15+
"""
16+
with mock.patch.dict("os.environ", {"STRANDS_TOOLS_DIR": str(tmp_file_structure["tools_dir"])}):
17+
test_query = "create a tool that can only calculate sum of two number called calculator"
18+
mock_stdin.return_value = io.StringIO("y\ny\ny\n")
19+
20+
with mock.patch.object(sys, "argv", ["strands", test_query]):
21+
strands.main()
22+
23+
agent = Agent(
24+
load_tools_from_directory=True,
25+
)
26+
27+
# assert agent has a tool called calculator
28+
assert hasattr(agent.tool, "calculator"), "Agent should have a 'calculator' tool after creation."
29+
30+
test_cases = [("what is 1 plus 2?", "3"), ("calculate 5 plus 7", "12")]
31+
32+
for question, expected in test_cases:
33+
response = agent(question)
34+
response_str = str(response).lower()
35+
assert expected in response_str, f"Expected '{expected}' in response for '{question}', got '{response_str}'"
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from unittest import mock
2+
3+
from strands_agents_builder import strands
4+
5+
6+
@mock.patch.object(strands, "get_user_input")
7+
@mock.patch.dict("os.environ", {"STRANDS_TOOL_CONSOLE_MODE": "enabled"})
8+
def test_cli_shell_and_interactive_mode(mock_user_input, capsys):
9+
"""Test interactive mode with a shell command."""
10+
11+
user_inputs = ["!echo hello", "exit"]
12+
mock_user_input.side_effect = user_inputs
13+
14+
strands.main()
15+
out = capsys.readouterr().out
16+
17+
assert "hello" in out, "Expected 'hello' (from echo) in output"
18+
assert "thank you for using strands" in out.lower(), "Expected exit message"

0 commit comments

Comments
 (0)