Testing Agent Decisions
Agent decision tests verify that the agent selects the right next action given a specific state. These tests use mocked language models to make results deterministic.
8 min•By Priygop Team•Updated 2026
Decision Test Pattern
Decision Test Pattern
# Testing agent decisions with a mock LLM
import pytest
from unittest.mock import MagicMock, patch
# The agent function we want to test
def agent_decide_next_action(goal: str, state: dict, available_tools: list) -> dict:
"""
In production: calls an LLM.
In tests: mocked to return predictable decisions.
Returns: {"action": str, "args": dict}
"""
# This would normally call the LLM
raise NotImplementedError("Use mock in tests")
class TestAgentDecisions:
"""Test that the agent makes correct decisions in known scenarios."""
def test_research_task_starts_with_search(self):
"""Given a research goal and no prior steps, agent should search first."""
mock_llm_response = {"action": "web_search", "args": {"query": "Python AI frameworks"}}
with patch("agent_decide_next_action", return_value=mock_llm_response):
result = mock_llm_response # Simulating the mock
assert result["action"] == "web_search"
assert "query" in result["args"]
assert len(result["args"]["query"]) > 0
def test_agent_finishes_when_goal_achieved(self):
"""Agent should return FINISH when goal_achieved is in state."""
state = {
"goal": "Find Python frameworks",
"data": {"goal_achieved": True, "result": "Django, FastAPI"},
"history": [{"action": "web_search"}, {"action": "summarise"}]
}
mock_response = {"action": "FINISH", "args": {}}
with patch("agent_decide_next_action", return_value=mock_response):
result = mock_response
assert result["action"] == "FINISH"
def test_agent_escalates_after_max_retries(self):
"""Agent should escalate when the retry count exceeds the limit."""
state = {
"goal": "Get product data",
"data": {},
"retry_counts": {"get_product": 3}
}
mock_response = {"action": "ESCALATE",
"args": {"reason": "Max retries exceeded"}}
with patch("agent_decide_next_action", return_value=mock_response):
result = mock_response
assert result["action"] == "ESCALATE"
# Run simple assertions (no pytest required for this demo)
print("Decision tests simulated:")
print(" ✓ research task starts with search")
print(" ✓ agent finishes when goal achieved")
print(" ✓ agent escalates after max retries")