闲社服务运行正常AI智能体自动化平台
p

PydanticAI测试

技能标识: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.

作者:admin | 来源记录:ClawHub
登记来源
ClawHub
版本
V 1.0.0
检测标记
后台标记通过
免费
免费获取
0
收藏
来源与检测标记为平台登记信息,并不代表已展示可复核的检测报告。使用前请核对版本、依赖和所需权限,建议先在隔离环境中运行。
概述
安装方式
版本历史

PydanticAI测试

测试 PydanticAI 代理

TestModel(确定性测试)

使用 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)

TestModel 配置

python
from pydantic_ai.models.test import TestModel

自定义文本输出

model = TestModel(customoutputtext=自定义响应) result = agent.run_sync(Hello, model=model) assert result.output == 自定义响应

自定义结构化输出(用于 output_type 代理)

from pydantic import BaseModel

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 == 测试

用于可重现随机输出的种子

model = TestModel(seed=42)

强制工具调用

model = TestModel(calltools=[mytool, another_tool])

覆盖上下文管理器

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

FunctionModel(自定义逻辑)

完全控制模型响应:

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)

带工具调用的 FunctionModel

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(获取数据)

VCR 磁带(录制的 API 调用)

录制和回放真实的 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()

录制/更新磁带:

uv run pytest --record-mode=rewrite tests/test_file.py

磁带文件以 YAML 格式存储在 tests/cassettes/ 中。

内联快照

使用自动更新的快照断言预期输出:

python
from inline_snapshot import snapshot

def testagentoutput():
result = agent.run_sync(Hello, model=TestModel())

# 首次运行:创建快照
# 后续运行:与快照进行断言
assert result.output == snapshot(此处为预期输出)

更新快照:

uv run pytest --inline-snapshot=fix

测试工具

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())

检查捕获的消息

for msg in messages: print(msg)

测试模式总结

场景方法
无 API 的单元测试TestModel()
自定义模型逻辑
FunctionModel(func) | | 录制的真实响应 | @pytest.mark.vcr | | 断言输出结构 | inline_snapshot | | 测试工具是否被调用 | TestModel(call_tools=[...]) | | 模拟依赖项 | agent.override(deps=...) |

pytest 配置

典型的 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 # 更新快照

标签

skill ai

通过对话安装

以下为平台配置的接入选项,并非逐项实测通过。能否安装取决于客户端支持、技能来源和运行环境:

OpenClaw WorkBuddy QClaw Kimi Claude

方式一:安装 SkillHub 和技能

帮我安装 SkillHub 和 pydantic-ai-testing-1776126321 技能

方式二:设置 SkillHub 为优先技能安装源

设置 SkillHub 为我的优先技能安装源,然后帮我安装 pydantic-ai-testing-1776126321 技能

通过命令行安装

skillhub install pydantic-ai-testing-1776126321

下载

⬇ 下载 pydantic-ai-testing v1.0.0(免费)

文件大小: 2.7 KB | 发布时间: 2026-4-14 11:19

v1.0.0 最新 2026-4-14 11:19
Initial release of pydantic-ai-testing.

- Enables deterministic testing of PydanticAI agents using TestModel.
- Supports custom model logic with FunctionModel for advanced scenarios.
- Integrates VCR cassettes to record and replay real API interactions in tests.
- Provides inline snapshot assertions for verifying outputs.
- Allows forced tool invocation and easy mocking of dependencies during testing.
- Includes utilities for capturing agent messages and structuring pytest test suites.
返回顶部