Test agents with mocked tools, error handling, loop prevention, and integration tests for real LLMs.
Testing Agent Reliability
import pytest
from unittest.mock import patch, MagicMock
# Test agent tool selection
def test_agent_uses_search_for_current_events():
with patch("tools.search_web") as mock_search:
mock_search.return_value = "Python 3.13 released Oct 2024"
result = agent.invoke({"input": "What are Python 3.13 features?"})
mock_search.assert_called_once()
assert "3.13" in result["output"]
# Test agent handles tool failure gracefully
def test_agent_handles_tool_error():
with patch("tools.search_web", side_effect=Exception("API timeout")):
result = agent.invoke({"input": "Search for news"})
# Agent should acknowledge failure, not crash
assert "error" in result["output"].lower() or "unable" in result["output"].lower()
# Test agent respects max iterations
def test_agent_does_not_loop_forever():
agent_with_limit = build_agent(max_iterations=5)
result = agent_with_limit.invoke({"input": "Impossible task"})
assert result is not None # did not hang
# Integration test with real LLM (expensive, run rarely)
@pytest.mark.integration
def test_full_agent_task():
result = agent.invoke({"input": "Calculate 15% of 847"})
assert "127.05" in result["output"]