In short.
Pydantic AI's unit testing guide is a practical tutorial for Python developers building AI agents, but its model substitutes do not assess the quality of real model answers. It explains how to exercise application logic through automatic or scripted model responses, inspect message exchanges and reuse overrides across tests. Its weather example shows how automatically generated inputs can leave a code branch uncovered. Runtime correctness, release compatibility, total costs and reuse rights are not established by the supplied page.
Researched and written by an AI model and checked by a second AI model. How it works
Coverage includes the complete page at [https://pydantic.dev/docs/ai/guides/testing/index.md], including its documentation-index and agent query-parameter notices. Linked API, evaluation, image-generation, batch-processing and third-party documentation is excluded. The weather application's imported dependencies are not supplied. No code was executed; runtime behavior, release compatibility, real provider interactions and end-to-end operation remain unverified.
The overview
Resource and scope
This is the Unit testing page in the Pydantic AI documentation at [https://pydantic.dev/docs/ai/guides/testing/index.md]. It teaches testing patterns for existing Python applications that use Pydantic AI agents.
The central distinction is between checking application behavior around a model, such as tools, dependencies and output handling, and evaluating the quality of the model's answers.
Practical value
The guide is useful for repeatable regression tests that should avoid real model requests. It provides concrete patterns for replacing the model inside application code, asserting stored results and inspecting the agent's message exchange.
Its strongest teaching example exposes a coverage gap: automatically generated tool arguments exercise the historical-weather path while leaving the forecast path untouched. The examples' runtime correctness and compatibility remain unverified; nothing was executed.
Who it helps
The primary audience is Python developers maintaining Pydantic AI agents, especially applications that call tools, receive dependencies or save outputs. Beginners need familiarity with Python functions, imports and basic testing; the examples also use asynchronous code and context managers.
Experienced developers can use the patterns to target specific branches, inspect structured message histories and share model overrides through pytest fixtures. People seeking evidence about real model accuracy need evaluations beyond these unit tests.
What the guide covers
TestModel supplies procedural test responses and, by default, calls the agent's function tools. FunctionModel lets developers script model behavior, including tool arguments and final responses. Agent.override can replace models, dependencies or toolsets within application logic.
The guide also covers capture_run_messages, custom output text, pytest fixtures and immediate in-memory batch processing. It recommends blocking non-test model requests with models.ALLOW_MODEL_REQUESTS = False and briefly points image-generation users toward TestImageGenerationModel.
Using it
Prerequisites and dependencies
The examples require Python, Pydantic AI and pytest. They use anyio to run asynchronous tests. dirty-equals supplies flexible assertions for timestamps and strings; inline-snapshot is suggested for long assertions.
The weather application imports fake_database and weather_service, whose implementations are not supplied. Consequently, the page is not a complete standalone project. Supported Python and Pydantic AI versions, package pins and environment requirements are unspecified.
How model substitution works
TestModel uses procedural Python logic to generate arguments from tool and output schemas. It does not interpret the prompt using AI. Its generated data aims to satisfy validation, but the guide says this succeeds in most cases rather than guaranteeing every schema.
FunctionModel instead calls a developer-supplied function with the conversation messages and AgentInfo. That function returns ModelResponse objects containing tool calls or text. A callable instance with an asynchronous __call__ method can retain state between requests. Agent.override scopes the substitution around the application call.
Applying the patterns
In an existing Pydantic AI project, configure pytest for asynchronous tests with anyio, set models.ALLOW_MODEL_REQUESTS = False and wrap the application call in agent.override(model=TestModel()). Use FunctionModel when a test needs particular tool arguments or a scripted exchange.
For agents configured with provider-executed native tools, the guide specifies agent.override(model=TestModel(), native_tools=[]) unless the test checks that those tools are passed to the model. Repeated overrides can be placed in a pytest fixture. Installation commands and pinned dependencies are not provided.
The weather application
The example application accepts prompt and user-ID pairs, runs a weather agent and stores each result in a database. Its weather tool chooses historical data or a forecast by comparing the requested date with date.today().
The TestModel example supplies a generated date of 2024-01-01 independently of the prompt, illustrating why the forecast branch can remain uncovered. The FunctionModel example extracts 2032-01-01 from the prompt, supplies London as the location and returns the tool result as text. These are documented assertions, not observed execution results.
Assessment & evidence
Limits of the approach
These substitutes do not demonstrate real model reasoning, answer quality or provider behavior. TestModel cannot emulate provider-executed native tools, and its generated arguments may be irrelevant to the prompt or miss important branches.
Batch requests complete immediately in memory, so these tests do not establish actual provider scheduling or completion behavior. Because TestModel invokes application tools, replacing the model alone should not be assumed to isolate database or service activity; that follows from the documented tool execution mechanism.
Choosing meaningful test cases
TestModel is a useful starting point when schema-generated arguments are sufficient to exercise a path. FunctionModel provides more control when correctness depends on particular dates, locations, tool sequences or final responses.
The weather example supports a practical lesson: an asserted output does not establish coverage of every branch. Map each intended behavior to explicit inputs and assertions, and inspect message history when the tool exchange itself matters.
Related testing choices
Within the guide, FunctionModel offers scripted control when TestModel's automatic data is insufficient. pytest provides the test harness, while dirty-equals and inline-snapshot are suggested assertion helpers.
For assessing model-answer quality, the page points to evaluations at [https://pydantic.dev/docs/ai/evals/evals/]. That linked material is outside the available page, so its methods and requirements are not assessed. No comparison with other agent frameworks is provided.
Source and strength of evidence
The evidence is the complete documentation page at [https://pydantic.dev/docs/ai/guides/testing/index.md], containing explanations and Python examples. Its captured source revision is sha256:bbecca1ea80f7cdb67cbbb906d7f3dc19648129b4c78f3dc1ee24704cd8c44ff.
The page supports descriptions of the documented APIs and intended test patterns. Its code assertions are examples, not independent test receipts, benchmarks or proof of compatibility. Linked pages and the missing application dependencies provide no additional evidence here.
Access & freshness
Costs and resource use
The guide presents TestModel and FunctionModel as ways to avoid the usage, latency and variability of real LLM calls during unit tests. That benefit depends on applying the substitutes and preventing unintended non-test model requests.
No prices, measured savings or total operating costs are given. Compute, continuous integration and any databases or services reached by application tools are not costed. Total cost is unknown.
Licensing and reuse
The supplied page does not state a licence for its documentation or code examples, redistribution permissions or attribution requirements. Those rights are unknown from this evidence. No separate package licence is established by the page.
Dates and compatibility
The document was captured on 2026-11-14 at 22:48:29.474 UTC. The page does not establish a publication date, last-update date or applicable Pydantic AI release. The capture timestamp does not establish when the instructions were written.
Compatibility with a particular installed release remains unverified. The weather example uses date.today(), so fixed dates such as 2032-01-01 require attention as time advances.
Practical next steps
Begin with one application path, substitute TestModel and assert the resulting application state. Add FunctionModel cases for branches that require deliberate inputs, and capture messages when tool arguments or response sequences are part of the requirement.
Block unintended model requests, provide controlled test dependencies where tools access services and keep date-sensitive cases current. Confirm APIs against the installed Pydantic AI release, and assess real model-answer quality separately through evaluations.
