Evaluation & Testing
How to Test an Agent's Tool Selection Path with pytest Mocks
When an agent chooses the wrong tool, a live model call can make the failure hard to reproduce. A focused pytest test can make one routing path deterministic by supplying a controlled function-call response and invoking the agent’s normal dispatch path.
The key is to test the behavior your application already promises: which registered handler runs, which parsed arguments it receives, and what observation comes back. This guide uses pytest’s monkeypatch to replace the model-call method before the agent runs, so the test exercises application-side parsing and dispatch without a live request through that patched method.

Quick answer
The short version: prepare a fake response whose output contains a function_call item, the intended tool name, and JSON-encoded arguments. Patch the model-call attribute where your agent looks it up, then call the agent’s normal entry point.
Use a mock or spy to check that the expected registered handler ran with the parsed arguments. Also assert that the returned observation matches your application’s existing contract; if the agent wraps the result, check the observation in its usual result field.
Run the test with pytest. If a check fails, inspect its comparison details: a missing handler call can indicate a routing mismatch, while unexpected arguments can point to parsing or dispatch behavior that differs from your expectation.
Remember that patching one method only avoids a live request through that method, so check for other unmocked network calls too.
Step-by-step guide
Identify the agent path and expected behavior
Identify the agent path and expected behavior (prerequisites)Pick one agent entry point for your test. It should already exist in your codebase as a real method you can invoke. Good candidates are the object or class that creates the model request, resolves which tool to run, and then dispatches it. I pick the class whose invoke or run method accepts a message and returns an observation, because that keeps the test scenario focused on one clear path rather than a workflow with multiple turns. Then I identify its model-call method, the attribute where it looks up the client, and the registered tool this input should select. Next, I confirm what arguments the chosen tool expects and what observation the handler should produce. This contract is essential: the regression test mirrors existing behavior rather than inventing new behavior. If your application parses the tool name from response.output, the expected name must match exactly what you registered and what the fake response will provide. I would also verify that the normal dispatch path can be invoked directly, without going through an extra service layer or an environment variable switch. That matters because if the test route depends on a mock-able seam outside the agent class itself, you are testing something other than the tool routing you intended. One caveat: if you cannot isolate a single invoke method cleanly, skip ahead to the next section and consolidate the entry point first, or accept a slightly larger scope with documented tradeoffs.
Sources: Function calling | OpenAI API; How to monkeypatch/mock modules and environments - pytest documentation.
Create a controlled function-call response
Build a fake response for the exact tool your test input should select. For an application that reads Responses API output items through attributes, a lightweight stand-in can look like this:
from types import SimpleNamespace
fake_response = SimpleNamespace(
output=[
SimpleNamespace(
type="function_call",
name="lookup_weather",
arguments='{"city":"Oslo","unit":"celsius"}',
)
]
)Use the registered tool’s actual name and arguments that match its expected input; the JSON-encoded arguments string should decode to the values your later assertions expect. This keeps the model’s choice predetermined while still exercising your application’s response parsing.
Adapt the object shape if your parser expects mappings or another response representation—the test double must match what your code reads, not just the documented field names.
Sources: Function calling | OpenAI API; How to monkeypatch/mock modules and environments - pytest documentation.
Patch the model call with pytest
Patch the exact attribute your agent looks up, and do it before calling the agent. If the code reaches the Responses API through agent.client.responses.create, for example:
def fake_create(*args, **kwargs):
return fake_response
monkeypatch.setattr(agent.client.responses, "create", fake_create)Here, fake_response is the controlled response prepared in the previous step. Change the attribute path to match your code: if the agent looks up a client or function through an imported module, patch that module’s attribute instead.
Patching a different reference will not intercept the call. Then invoke the agent; the replacement returns the prepared response without making a live request through the patched method.
pytest restores the original attribute when the test finishes, so the patch does not persist into other tests.
Sources: Function calling | OpenAI API; How to monkeypatch/mock modules and environments - pytest documentation.
Invoke the agent’s normal dispatch path
With the controlled response and patch in place, call the agent’s existing entry point using the test input you chose. Use the method and input format your application actually supports; for example:
result = agent.invoke("What's the weather in Oslo?")This should take the agent through its ordinary response-parsing and dispatch path, with the patched model call supplying the predetermined function call. The application—not the model API—selects and runs the registered handler, so this step checks that your test reaches the routing code rather than calling the handler directly.
Keep the returned value for the next step, where you’ll check the tool arguments and observation against your application’s contract. If your entry point is named run or returns a structured result instead, use that existing interface and inspect the observation from its normal result shape.
Sources: Function calling | OpenAI API; How to monkeypatch/mock modules and environments - pytest documentation.
Check the selected tool, arguments, and observation
Use a mock or spy for the registered handler so you can see whether the agent dispatched to the intended tool. Keep the handler in the normal registry and give it a return value that matches the observation your application is expected to produce.
weather_handler.assert_called_once_with(city="Oslo", unit="celsius")
assert result == expected_observationThe first assertion checks both tool selection and the parsed arguments; the second checks the returned observation against your agent’s existing contract. If the entry point wraps the observation in a result object, assert against its observation field instead, and adjust the expected call arguments if your handler uses positional arguments.
Run the test and inspect pytest’s comparison details if an assertion fails. A missing handler call points to a dispatch or tool-name mismatch, while unexpected arguments can indicate a parsing or routing regression.
Sources: Function calling | OpenAI API; How to monkeypatch/mock modules and environments - pytest documentation; How to write and report assertions in tests - pytest documentation.
Run the regression test and inspect its assertions
From your project root, run the test with pytest, substituting its actual file path:
pytest path/to/test_agent.pyA passing result means the assertions in this test matched the expected tool call, parsed arguments, and observation; it does not establish behavior outside this path. If a check fails, read pytest’s comparison details to see which actual value differed from the expected one.
A missing or unexpected handler call suggests the routing result differs from your test’s expectation. If the handler call is correct but the observation check fails, compare the returned value with the application’s existing observation contract, including any wrapper around the result.
Sources: Function calling | OpenAI API; How to monkeypatch/mock modules and environments - pytest documentation; How to write and report assertions in tests - pytest documentation.
Diagnose patching and routing failures
If the test still makes a live model request, check that monkeypatch.setattr targets the exact client or function attribute your agent looks up. Apply the patch before invoking the agent, and check for any other network calls that the patched method does not cover.
If the expected tool does not run, compare the fake response with the shape your parser reads. For a Responses API function call, check that the output item has type="function_call", the registered tool’s exact name, and JSON-encoded arguments.
Then inspect pytest’s assertion details. A missing handler call can point to a dispatch or tool-name mismatch; unexpected call arguments can point to a parsing or routing discrepancy.
If the handler call matches but the observation assertion fails, compare the actual result with your application’s existing observation contract, including any wrapper around it.
Sources: Function calling | OpenAI API; How to monkeypatch/mock modules and environments - pytest documentation; How to write and report assertions in tests - pytest documentation.
Next steps
A useful tool-selection regression test keeps the model’s choice fixed while leaving your application’s response parsing and dispatch in the path. That makes the result easier to diagnose than a test whose outcome also depends on a live model response.
Before relying on the test, confirm that the patch targets the attribute your agent actually looks up and that it is applied before invocation. Then verify the handler, parsed arguments, and observation against the application’s existing behavior.
If the assertions pass, you have checked this specific routing path; use pytest’s failure details to investigate any mismatch, and add separate tests for other paths you need to protect.