Documentation
¶
Overview ¶
Package testing 提供测试工具支持,用于 enhance 框架。
该模块提供测试辅助函数、模拟对象创建、测试断言等测试相关功能,简化单元测试和集成测试的编写。
架构设计 ¶
- TestingT: 测试接口,兼容 testing.T
- TestRunner: 测试运行器接口,用于运行测试
- TestContext: 测试上下文接口,提供测试环境
- Mock: Mock 对象接口,用于设置期望和验证调用
核心功能 ¶
- 断言函数: 提供 Equal、NotNil、True、False 等常用断言
- 模拟对象: 支持创建模拟依赖对象
- 测试辅助: 提供临时文件创建、随机数据生成等辅助函数
- 测试超时: 支持设置测试超时时间
使用方式 ¶
使用断言:
func TestUserService(t *testing.T) {
user := service.GetUser(1)
testing.AssertNotNil(t, user, "user should not be nil")
testing.AssertEqual(t, "John", user.Name, "name should match")
}
使用测试辅助:
func TestFileProcessing(t *testing.T) {
tmpFile := testing.CreateTempFile(t, "test content")
defer tmpFile.Close()
// 测试文件处理逻辑
}
断言函数 ¶
- AssertEqual: 断言两个值相等
- AssertNotEqual: 断言两个值不相等
- AssertNil: 断言值为 nil
- AssertNotNil: 断言值不为 nil
- AssertTrue: 断言条件为 true
- AssertFalse: 断言条件为 false
- AssertContains: 断言字符串包含子串
- AssertPanics: 断言函数会 panic
Index ¶
- func Assert(t *testing.T, condition bool, msg string)
- func AssertEqual(t *testing.T, expected, actual any, msg ...string)
- func AssertError(t *testing.T, err error, msg ...string)
- func AssertExpectations(t TestingT, mock Mock) bool
- func AssertFalse(t *testing.T, condition bool, msg ...string)
- func AssertNil(t *testing.T, value any, msg ...string)
- func AssertNoError(t *testing.T, err error, msg ...string)
- func AssertNotNil(t *testing.T, value any, msg ...string)
- func AssertTrue(t *testing.T, condition bool, msg ...string)
- func GetByType[T any](ctx TestContext) (T, error)
- func MustGetByType[T any](ctx TestContext) T
- func Parallel(t *testing.T, tests map[string]func(ctx TestContext))
- func RunSubtest(t *testing.T, name string, fn func(ctx TestContext))
- func SkipIf(t *testing.T, condition bool, reason string)
- func TeardownTest(ctx TestContext, teardown func(ctx TestContext))
- func Test(t *testing.T, fn func(ctx TestContext))
- func TestWithContainer(t *testing.T, container core.Container, fn func(ctx TestContext))
- func WithMock(t *testing.T, fn func(ctx TestContext, mock *MockRecorder))
- type Expectation
- type Mock
- type MockRecorder
- type TestConfig
- type TestContext
- type TestOption
- type TestResponse
- type TestRunner
- type TestWebClient
- type TestingT
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AssertEqual ¶
AssertEqual 断言两个值相等。
func AssertExpectations ¶
AssertExpectations 断言 Mock 期望是否满足。
func AssertFalse ¶
AssertFalse 断言条件为假。
func AssertNoError ¶
AssertNoError 断言无错误。
func AssertNotNil ¶
AssertNotNil 断言值不为 nil。
func MustGetByType ¶
func MustGetByType[T any](ctx TestContext) T
MustGetByType 必须获取指定类型的 Bean,否则测试失败。
func Parallel ¶
func Parallel(t *testing.T, tests map[string]func(ctx TestContext))
Parallel 并行运行多个测试。
func RunSubtest ¶
func RunSubtest(t *testing.T, name string, fn func(ctx TestContext))
RunSubtest 运行子测试。
func TeardownTest ¶
func TeardownTest(ctx TestContext, teardown func(ctx TestContext))
TeardownTest 清理测试环境。
func TestWithContainer ¶
func TestWithContainer(t *testing.T, container core.Container, fn func(ctx TestContext))
TestWithContainer 使用指定容器运行测试。
func WithMock ¶
func WithMock(t *testing.T, fn func(ctx TestContext, mock *MockRecorder))
WithMock 使用 Mock 运行测试。
Types ¶
type Expectation ¶
Expectation 表示一个方法调用期望。
type Mock ¶
type Mock interface {
// Expect 设置方法调用期望,默认期望调用 1 次。
Expect(method string, args []any, result any, err error) Mock
// ExpectTimes 设置方法调用期望,指定期望调用次数。
ExpectTimes(method string, args []any, result any, err error, times int) Mock
// Call 模拟方法调用,返回匹配的期望结果。
Call(method string, args ...any) (any, error)
// Verify 验证所有期望是否满足。
Verify() error
// Reset 重置 Mock 对象的所有状态。
Reset()
}
Mock 模拟对象接口。
用于设置方法调用期望和验证调用是否满足。
type MockRecorder ¶
type MockRecorder struct {
// contains filtered or unexported fields
}
MockRecorder Mock 记录器,用于链式设置期望。
type TestConfig ¶
type TestConfig struct {
Properties map[string]any
MockBeans map[string]any
AutoConfig bool
AppName string
}
TestConfig 测试配置
type TestContext ¶
type TestContext interface {
// T 获取底层测试对象。
T() TestingT
// GetByType 从容器按类型获取 Bean,如果获取失败则测试失败。
GetByType(t reflect.Type) any
// Register 向容器注册 Bean。
Register(name string, bean any)
// SetProperty 设置测试属性。
SetProperty(key string, value any)
// GetProperty 获取测试属性。
GetProperty(key string) any
// AddCleanup 添加测试清理函数。
AddCleanup(fn func())
// Cleanup 执行所有清理函数。
Cleanup()
// Close 关闭测试上下文。
Close()
// Container 获取 IoC 容器。
Container() any
}
TestContext 测试上下文接口。
提供测试环境,包括 IoC 容器、测试配置、清理函数等。
func SetupTest ¶
func SetupTest(t *testing.T, setup func(ctx TestContext)) TestContext
SetupTest 设置测试环境。
type TestResponse ¶
type TestResponse struct {
// contains filtered or unexported fields
}
TestResponse 测试响应
func (*TestResponse) AssertBody ¶
func (r *TestResponse) AssertBody(t *testing.T, expected string)
AssertBody 断言响应体
func (*TestResponse) AssertStatus ¶
func (r *TestResponse) AssertStatus(t *testing.T, expected int)
AssertStatus 断言状态码
type TestRunner ¶
type TestRunner struct {
// contains filtered or unexported fields
}
TestRunner 测试运行器
func NewTestRunner ¶
func NewTestRunner(t *testing.T, opts ...TestOption) *TestRunner
NewTestRunner 创建测试运行器
type TestWebClient ¶
type TestWebClient struct {
// contains filtered or unexported fields
}
TestWebClient Web 测试客户端
func NewTestWebClient ¶
func NewTestWebClient(t *testing.T, baseURL string) *TestWebClient
NewTestWebClient 创建 Web 测试客户端
func (*TestWebClient) Post ¶
func (c *TestWebClient) Post(path string, body any) *TestResponse
Post 发送 POST 请求