testutil

package
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 24, 2026 License: MIT Imports: 7 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func AssertContains

func AssertContains(t *testing.T, s, substr string)

AssertContains 断言字符串包含子串

func AssertError

func AssertError(t *testing.T, err error, msgAndArgs ...any)

AssertError 断言有错误

func AssertEventuallyEqual

func AssertEventuallyEqual(t *testing.T, expected any, getter func() any, timeout time.Duration)

AssertEventuallyEqual 断言值最终相等

func AssertEventuallyTrue

func AssertEventuallyTrue(t *testing.T, condition func() bool, timeout time.Duration)

AssertEventuallyTrue 断言条件最终为真

func AssertJSONEqual

func AssertJSONEqual(t *testing.T, expected, actual any)

AssertJSONEqual 断言两个值的 JSON 表示相等

func AssertMessagesEqual

func AssertMessagesEqual(t *testing.T, expected, actual []types.Message)

AssertMessagesEqual 断言两个消息切片相等

func AssertNoError

func AssertNoError(t *testing.T, err error, msgAndArgs ...any)

AssertNoError 断言没有错误

func AssertNotContains

func AssertNotContains(t *testing.T, s, substr string)

AssertNotContains 断言字符串不包含子串

func AssertToolCallsEqual

func AssertToolCallsEqual(t *testing.T, expected, actual []types.ToolCall)

AssertToolCallsEqual 断言两个工具调用切片相等

func CancelledContext

func CancelledContext() context.Context

CancelledContext 返回已取消的上下文

func CollectStreamChunks

func CollectStreamChunks(ch <-chan llm.StreamChunk) []llm.StreamChunk

CollectStreamChunks 收集流式块到切片

func CollectStreamContent

func CollectStreamContent(ch <-chan llm.StreamChunk) string

CollectStreamContent 收集流式内容到字符串

func CopyMessages

func CopyMessages(messages []types.Message) []types.Message

CopyMessages 深拷贝消息切片

func CopyToolCalls

func CopyToolCalls(toolCalls []types.ToolCall) []types.ToolCall

CopyToolCalls 深拷贝工具调用切片

func MustJSON

func MustJSON(v any) string

MustJSON 将值转换为 JSON 字符串,失败时 panic

func MustParseJSON

func MustParseJSON[T any](s string) T

MustParseJSON 解析 JSON 字符串,失败时 panic

func SendChunksToChannel

func SendChunksToChannel(chunks []llm.StreamChunk) <-chan llm.StreamChunk

SendChunksToChannel 发送块到通道

func TestContext

func TestContext(t *testing.T) context.Context

TestContext 返回带超时的测试上下文

func TestContextWithTimeout

func TestContextWithTimeout(t *testing.T, timeout time.Duration) context.Context

TestContextWithTimeout 返回带自定义超时的测试上下文

func WaitFor

func WaitFor(condition func() bool, timeout time.Duration) bool

WaitFor 等待条件满足或超时

func WaitForChannel

func WaitForChannel[T any](ch <-chan T, timeout time.Duration) (T, bool)

WaitForChannel 等待通道接收或超时

Types

type BenchmarkHelper

type BenchmarkHelper struct {
	// contains filtered or unexported fields
}

BenchmarkHelper 基准测试辅助结构

func NewBenchmarkHelper

func NewBenchmarkHelper(b *testing.B) *BenchmarkHelper

NewBenchmarkHelper 创建基准测试辅助

func (*BenchmarkHelper) ReportAllocs

func (h *BenchmarkHelper) ReportAllocs()

ReportAllocs 报告内存分配

func (*BenchmarkHelper) ResetTimer

func (h *BenchmarkHelper) ResetTimer()

ResetTimer 重置计时器

func (*BenchmarkHelper) RunParallel

func (h *BenchmarkHelper) RunParallel(body func())

RunParallel 并行运行基准测试

func (*BenchmarkHelper) StartTimer

func (h *BenchmarkHelper) StartTimer()

StartTimer 启动计时器

func (*BenchmarkHelper) StopTimer

func (h *BenchmarkHelper) StopTimer()

StopTimer 停止计时器

Directories

Path Synopsis
fixtures 中的 Agent 测试数据工厂。
fixtures 中的 Agent 测试数据工厂。
MockMemoryManager 的测试模拟实现。
MockMemoryManager 的测试模拟实现。

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL