技能标识:pydantic-ai-testing
Test PydanticAI agents using TestModel, FunctionModel, VCR cassettes, and inline snapshots. Use when writing unit tests, mocking LLM responses, or recording API interactions.
使用 TestModel 进行无需 API 调用的测试:
python
import pytest
from pydantic_ai import Agent
from pydantic_ai.models.test import TestModel
def testagentbasic():
agent = Agent(openai:gpt-4o)
# 使用 TestModel 覆盖进行测试
result = agent.run_sync(Hello, model=TestModel())
# TestModel 基于 output_type 生成确定性输出
assert isinstance(result.output, str)
python
from pydantic_ai.models.test import TestModel
class Response(BaseModel):
message: str
score: int
agent = Agent(openai:gpt-4o, output_type=Response)
model = TestModel(customoutputargs={message: 测试, score: 42})
result = agent.run_sync(Hello, model=model)
assert result.output.message == 测试
python
from pydantic_ai import Agent
from pydantic_ai.models.test import TestModel
agent = Agent(openai:gpt-4o, deps_type=MyDeps)
def testwithoverride():
mock_deps = MyDeps(db=MockDB())
with agent.override(model=TestModel(), deps=mock_deps):
# 所有运行都使用 TestModel 和 mock_deps
result = agent.run_sync(Hello)
assert result.output
完全控制模型响应:
python
from pydantic_ai import Agent, ModelMessage, ModelResponse, TextPart
from pydantic_ai.models.function import AgentInfo, FunctionModel
def custom_model(
messages: list[ModelMessage],
info: AgentInfo
) -> ModelResponse:
检查消息并返回响应的自定义模型。
# 访问最后一条用户消息
last_msg = messages[-1]
# 返回自定义响应
return ModelResponse(parts=[TextPart(自定义响应)])
agent = Agent(FunctionModel(custom_model))
result = agent.run_sync(Hello)
python
from pydantic_ai import ToolCallPart, ModelResponse
from pydantic_ai.models.function import AgentInfo, FunctionModel
def modelwithtools(
messages: list[ModelMessage],
info: AgentInfo
) -> ModelResponse:
# 第一次请求:调用工具
if len(messages) == 1:
return ModelResponse(parts=[
ToolCallPart(
toolname=getdata,
args={id: 123}
)
])
# 工具响应后:返回最终结果
return ModelResponse(parts=[TextPart(使用工具结果完成)])
agent = Agent(FunctionModel(modelwithtools))
@agent.tool_plain
def get_data(id: int) -> str:
return fID {id} 的数据
result = agent.run_sync(获取数据)
录制和回放真实的 LLM API 交互:
python
import pytest
@pytest.mark.vcr
def testwithrecorded_response():
使用 tests/cassettes/ 中的录制磁带
agent = Agent(openai:gpt-4o)
result = agent.run_sync(Hello)
assert hello in result.output.lower()
磁带文件以 YAML 格式存储在 tests/cassettes/ 中。
使用自动更新的快照断言预期输出:
python
from inline_snapshot import snapshot
def testagentoutput():
result = agent.run_sync(Hello, model=TestModel())
# 首次运行:创建快照
# 后续运行:与快照进行断言
assert result.output == snapshot(此处为预期输出)
python
from pydantic_ai import Agent, RunContext
from pydantic_ai.models.test import TestModel
def testtoolis_called():
agent = Agent(openai:gpt-4o)
tool_called = False
@agent.tool_plain
def my_tool(x: int) -> str:
nonlocal tool_called
tool_called = True
return f结果: {x}
# 强制 TestModel 调用工具
result = agent.run_sync(
使用 my_tool,
model=TestModel(calltools=[mytool])
)
assert tool_called
python
from dataclasses import dataclass
from unittest.mock import AsyncMock
@dataclass
class Deps:
api: ApiClient
def testtoolwith_deps():
# 创建模拟依赖
mock_api = AsyncMock()
mockapi.fetch.returnvalue = {data: 测试}
agent = Agent(openai:gpt-4o, deps_type=Deps)
@agent.tool
async def fetch_data(ctx: RunContext[Deps]) -> dict:
return await ctx.deps.api.fetch()
with agent.override(
model=TestModel(calltools=[fetchdata]),
deps=Deps(api=mock_api)
):
result = agent.run_sync(获取数据)
mockapi.fetch.assertcalled_once()
检查运行中的所有消息:
python
from pydanticai import Agent, capturerun_messages
agent = Agent(openai:gpt-4o)
with capturerunmessages() as messages:
result = agent.run_sync(Hello, model=TestModel())
| 场景 | 方法 |
|---|---|
| 无 API 的单元测试 | TestModel() |
| 自定义模型逻辑 |
典型的 pyproject.toml:
toml
[tool.pytest.ini_options]
testpaths = [tests]
asyncio_mode = auto # 用于异步测试
运行测试:
bash
uv run pytest tests/test_agent.py -v
uv run pytest --inline-snapshot=fix # 更新快照
以下为平台配置的接入选项,并非逐项实测通过。能否安装取决于客户端支持、技能来源和运行环境:
帮我安装 SkillHub 和 pydantic-ai-testing-1776126321 技能
设置 SkillHub 为我的优先技能安装源,然后帮我安装 pydantic-ai-testing-1776126321 技能
skillhub install pydantic-ai-testing-1776126321
文件大小: 2.7 KB | 发布时间: 2026-4-14 11:19