Testing the Agent with Examples › Running the Tests

Michael BrenndoerferAugust 2, 20253 min read
AI Agent HandbookAll chapters
Part 11 of 15Evaluation - Measuring Your Agent's Performance
Chapter 31Testing the Agent with Examples › Running the Tests
Reading section 3 of 11 · Running the Tests · 3 minSection 3 of 11 · 3 min

Section 3 of 11 · 3 min read

Reading settings

Notes & comments

With our test suite ready, we need a way to run each test and check whether the agent passes. Let's build a simple test runner:

PythonIn [5]
CollapseExpand
def run_test(agent, test_case):
    """
    Run a single test case and return the results.
    """
    print(f"\nRunning test: {test_case['id']}")
    print(f"Input: {test_case['input']}")

    # Run the agent with the test input
    response = agent.process(test_case["input"])

    # Record what happened
    result = {
        "test_id": test_case["id"],
        "input": test_case["input"],
        "output": response["text"],
        "behavior": response.get("actions_taken", []),
        "passed": False,
        "notes": [],
    }

    # Check if the agent's behavior matches expectations
    if test_case["expected_behavior"] in str(response.get("actions_taken", [])):
        result["notes"].append(
            f"✓ Correct behavior: {test_case['expected_behavior']}"
        )
    else:
        result["notes"].append(
            f"✗ Expected {test_case['expected_behavior']}, got {response.get('actions_taken', [])}"
        )

    # Check if the output matches expectations
    if test_case["expected_output"].lower() in response["text"].lower():
        result["notes"].append("✓ Correct output")
        result["passed"] = True
    else:
        result["notes"].append(
            f"✗ Output doesn't match expected: {test_case['expected_output']}"
        )

    return result

This test runner does a few important things. First, it runs the agent with the test input and captures the response. Then it checks two things: did the agent use the right approach (behavior), and did it produce the right answer (output)? Both checks need to pass for the test to succeed.

The checking logic here is intentionally simple. We're looking for substrings and checking if certain actions were taken. In a production system, you might want more sophisticated matching, but this is enough to get started.

Let's run our entire test suite:

PythonIn [6]
CollapseExpand
def run_test_suite(agent, test_suite):
    """
    Run all tests and generate a report.
    """
    results = []

    for test_case in test_suite:
        # Check if this test has dependencies
        if "requires" in test_case:
            # Make sure required tests ran first
            for required_id in test_case["requires"]:
                required_result = next(
                    (r for r in results if r["test_id"] == required_id), None
                )
                if not required_result or not required_result["passed"]:
                    print(
                        f"Skipping {test_case['id']} - dependency {required_id} failed"
                    )
                    continue

        result = run_test(agent, test_case)
        results.append(result)

        # Print immediate feedback
        status = "PASS" if result["passed"] else "FAIL"
        print(f"Result: {status}")
        for note in result["notes"]:
            print(f"  {note}")

    return results

When you run this, you'll see output like this:

Code
CollapseExpand
Running test: math_001
Input: What's 1,234 multiplied by 5,678?
Result: PASS
  ✓ Correct behavior: uses_calculator
  ✓ Correct output

Running test: memory_001
Input: Remember that my birthday is July 20th.
Result: PASS
  ✓ Correct behavior: stores_to_memory
  ✓ Correct output

Running test: memory_002
Input: When is my birthday?
Result: PASS
  ✓ Correct behavior: retrieves_from_memory
  ✓ Correct output

Running test: reasoning_001
Input: If I have a meeting at 2 PM and it takes 45 minutes to get there, when should I leave?
Result: PASS
  ✓ Correct behavior: step_by_step_reasoning
  ✓ Correct output

Running test: planning_001
Input: Find today's weather and recommend what I should wear.
Result: FAIL
  ✓ Correct behavior: uses_weather_tool_then_makes_recommendation
  ✗ Output doesn't match expected: weather report followed by clothing suggestion

In this example, four tests passed and one failed. The planning test shows the right behavior (it called the weather tool and made a recommendation), but the output format didn't match what we expected. This is valuable feedback. Maybe our expected output was too strict, or maybe the agent needs to format its responses more consistently.

Comments

Add to the conversation

A question, a correction, or an example from your own work.

No comments yet. Be the first to share your thoughts!

Newsletter

Stay up to date

Get new articles, book updates, and notes on AI and technology.

Unsubscribe at any time. Privacy policy