Documentation
¶
Overview ¶
Package testutil 提供 AgentFlow 测试的共享工具和辅助函数。
概述 ¶
testutil 包为整个项目的单元测试与基准测试提供统一的辅助能力, 避免各包重复实现相似的测试基础设施。所有测试应优先使用此包 中的工具函数和 Mock 实现。
核心能力 ¶
- 上下文辅助: TestContext / TestContextWithTimeout / CancelledContext, 自动注册 Cleanup 防止泄漏
- 断言工具: AssertMessagesEqual / AssertToolCallsEqual / AssertJSONEqual / AssertNoError / AssertError / AssertContains 等
- 异步断言: AssertEventuallyTrue / AssertEventuallyEqual, 支持超时轮询等待条件满足
- 数据工具: MustJSON / MustParseJSON / CopyMessages / CopyToolCalls, 简化测试数据构造与深拷贝
- 流式辅助: CollectStreamChunks / CollectStreamContent / SendChunksToChannel,用于 LLM 流式响应测试
- 基准辅助: BenchmarkHelper 封装 testing.B 常用操作
子包 ¶
- testutil/mocks: Mock 实现,包括 MockProvider(LLM Provider)、 MockMemoryManager(记忆管理器)、MockToolManager(工具管理器), 均支持 Builder 模式与错误注入
- testutil/fixtures: 测试数据工厂,提供预置 Agent 配置、 ChatResponse、StreamChunk、ToolSchema、对话历史等样例
使用示例 ¶
ctx := testutil.TestContext(t)
provider := mocks.NewMockProvider().WithResponse("hello")
resp, err := provider.Completion(ctx, req)
testutil.AssertNoError(t, err)
testutil 通用测试辅助与断言工具。
提供上下文构造、消息断言及异步等待断言等能力。
Index ¶
- func AssertContains(t *testing.T, s, substr string)
- func AssertError(t *testing.T, err error, msgAndArgs ...any)
- func AssertEventuallyEqual(t *testing.T, expected any, getter func() any, timeout time.Duration)
- func AssertEventuallyTrue(t *testing.T, condition func() bool, timeout time.Duration)
- func AssertJSONEqual(t *testing.T, expected, actual any)
- func AssertMessagesEqual(t *testing.T, expected, actual []types.Message)
- func AssertNoError(t *testing.T, err error, msgAndArgs ...any)
- func AssertNotContains(t *testing.T, s, substr string)
- func AssertToolCallsEqual(t *testing.T, expected, actual []types.ToolCall)
- func CancelledContext() context.Context
- func CollectStreamChunks(ch <-chan llm.StreamChunk) []llm.StreamChunk
- func CollectStreamContent(ch <-chan llm.StreamChunk) string
- func CopyMessages(messages []types.Message) []types.Message
- func CopyToolCalls(toolCalls []types.ToolCall) []types.ToolCall
- func MustJSON(v any) string
- func MustParseJSON[T any](s string) T
- func SendChunksToChannel(chunks []llm.StreamChunk) <-chan llm.StreamChunk
- func TestContext(t *testing.T) context.Context
- func TestContextWithTimeout(t *testing.T, timeout time.Duration) context.Context
- func WaitFor(condition func() bool, timeout time.Duration) bool
- func WaitForChannel[T any](ch <-chan T, timeout time.Duration) (T, bool)
- type BenchmarkHelper
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AssertEventuallyEqual ¶
AssertEventuallyEqual 断言值最终相等
func AssertEventuallyTrue ¶
AssertEventuallyTrue 断言条件最终为真
func AssertJSONEqual ¶
AssertJSONEqual 断言两个值的 JSON 表示相等
func AssertMessagesEqual ¶
AssertMessagesEqual 断言两个消息切片相等
func AssertNoError ¶
AssertNoError 断言没有错误
func AssertNotContains ¶
AssertNotContains 断言字符串不包含子串
func AssertToolCallsEqual ¶
AssertToolCallsEqual 断言两个工具调用切片相等
func CollectStreamChunks ¶
func CollectStreamChunks(ch <-chan llm.StreamChunk) []llm.StreamChunk
CollectStreamChunks 收集流式块到切片
func CollectStreamContent ¶
func CollectStreamContent(ch <-chan llm.StreamChunk) string
CollectStreamContent 收集流式内容到字符串
func CopyMessages ¶
CopyMessages 深拷贝消息切片
func CopyToolCalls ¶
CopyToolCalls 深拷贝工具调用切片
func SendChunksToChannel ¶
func SendChunksToChannel(chunks []llm.StreamChunk) <-chan llm.StreamChunk
SendChunksToChannel 发送块到通道
func TestContextWithTimeout ¶
TestContextWithTimeout 返回带自定义超时的测试上下文
Types ¶
type BenchmarkHelper ¶
type BenchmarkHelper struct {
// contains filtered or unexported fields
}
BenchmarkHelper 基准测试辅助结构
func NewBenchmarkHelper ¶
func NewBenchmarkHelper(b *testing.B) *BenchmarkHelper
NewBenchmarkHelper 创建基准测试辅助
func (*BenchmarkHelper) RunParallel ¶
func (h *BenchmarkHelper) RunParallel(body func())
RunParallel 并行运行基准测试
Click to show internal directories.
Click to hide internal directories.