Documentation
¶
Overview ¶
Package test 提供测试支持工具和辅助函数
Package test 提供测试支持工具和辅助函数 ¶
Package test 提供测试支持工具和辅助函数 ¶
Package test 提供测试支持工具和辅助函数 ¶
Package test 提供测试支持工具和辅助函数
Index ¶
- func RunIntegrationTests(t *testing.T, suite IntegrationTestSuite, tests map[string]func(t *testing.T))
- type HTTPClient
- func (c *HTTPClient) DELETE(path string, headers map[string]string) *Response
- func (c *HTTPClient) Do(req Request) *Response
- func (c *HTTPClient) GET(path string, query url.Values, headers map[string]string) *Response
- func (c *HTTPClient) PATCH(path string, body interface{}, headers map[string]string) *Response
- func (c *HTTPClient) POST(path string, body interface{}, headers map[string]string) *Response
- func (c *HTTPClient) PUT(path string, body interface{}, headers map[string]string) *Response
- func (c *HTTPClient) UploadFile(path, fieldName, filePath string, additionalFields map[string]string) *Response
- func (c *HTTPClient) WithBaseURL(baseURL string) *HTTPClient
- func (c *HTTPClient) WithHeader(key, value string) *HTTPClient
- func (c *HTTPClient) WithHeaders(headers map[string]string) *HTTPClient
- type Helper
- func (h *Helper) AssertContainsAll(str string, substrings ...string)
- func (h *Helper) AssertNotContainsAny(str string, substrings ...string)
- func (h *Helper) CleanupTestDB(db *gorm.DB)
- func (h *Helper) GetProjectRoot() string
- func (h *Helper) LoadFixtureFile(relativePath string) []byte
- func (h *Helper) RandomString(length int) string
- func (h *Helper) RunParallel(fn func(h *Helper))
- func (h *Helper) SetupAndTeardown(setup func(), teardown func())
- func (h *Helper) SetupTestDB() *gorm.DB
- func (h *Helper) TempDir() string
- func (h *Helper) TempFile(pattern string, content []byte) string
- type IntegrationTest
- func (i *IntegrationTest) CreateFixturesDir() string
- func (i *IntegrationTest) CreateTestFixtures(fixturePath string, modelType interface{})
- func (i *IntegrationTest) ExecuteSQL(query string, args ...interface{})
- func (i *IntegrationTest) MigrateTestModels(models ...interface{})
- func (i *IntegrationTest) NewHTTPTest() *HTTPClient
- func (i *IntegrationTest) RunWithTimeout(timeout time.Duration, fn func(ctx context.Context))
- func (i *IntegrationTest) SetupRepositoryWithTx(modelType interface{}) *db.GenericRepository[interface{}]
- func (i *IntegrationTest) SetupTestApp() *flow.Engine
- func (i *IntegrationTest) SetupTestDB() *gorm.DB
- func (i *IntegrationTest) SetupTestTransaction() *sql.Tx
- func (i *IntegrationTest) TruncateTable(tableName string)
- type IntegrationTestSuite
- type Mock
- type MockAssertions
- type MockGenerator
- func (g *MockGenerator) FillStruct(s interface{})
- func (g *MockGenerator) RandomBool() bool
- func (g *MockGenerator) RandomDate(startYear, endYear int) time.Time
- func (g *MockGenerator) RandomElement(slice interface{}) interface{}
- func (g *MockGenerator) RandomEmail() string
- func (g *MockGenerator) RandomFloat(min, max float64) float64
- func (g *MockGenerator) RandomInt(min, max int) int
- func (g *MockGenerator) RandomName() string
- func (g *MockGenerator) RandomPhoneNumber() string
- func (g *MockGenerator) RandomString(length int) string
- func (g *MockGenerator) RandomSubset(slice interface{}, n int) interface{}
- func (g *MockGenerator) WithSeed(seed int64) *MockGenerator
- type Request
- type Response
- func (r *Response) AssertHeader(t *testing.T, key, value string) *Response
- func (r *Response) AssertHeaderContains(t *testing.T, key, value string) *Response
- func (r *Response) AssertJSON(t *testing.T, expected interface{}) *Response
- func (r *Response) AssertJSONContains(t *testing.T, key string, value interface{}) *Response
- func (r *Response) AssertStatus(t *testing.T, status int) *Response
- func (r *Response) BindJSON(v interface{}) error
- func (r *Response) JSON() (map[string]interface{}, error)
- func (r *Response) String() string
- type UnitTest
- func (u *UnitTest) AssertJSONEqual(expected, actual string)
- func (u *UnitTest) CallMethod(obj interface{}, methodName string, args ...interface{}) []interface{}
- func (u *UnitTest) FromJSON(jsonStr string, obj interface{})
- func (u *UnitTest) GetFunctionName(i interface{}) string
- func (u *UnitTest) GetTestName() string
- func (u *UnitTest) RunTestCase(name string, fn func(t *testing.T))
- func (u *UnitTest) RunTestCases(testCases map[string]func(t *testing.T))
- func (u *UnitTest) ToJSON(obj interface{}) string
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func RunIntegrationTests ¶
func RunIntegrationTests(t *testing.T, suite IntegrationTestSuite, tests map[string]func(t *testing.T))
RunIntegrationTests 运行集成测试套件中的测试
Types ¶
type HTTPClient ¶
type HTTPClient struct {
Engine *flow.Engine
T *testing.T
BaseURL string
Headers map[string]string
}
HTTPClient 是用于测试HTTP请求的客户端
func NewHTTPClient ¶
func NewHTTPClient(t *testing.T, engine *flow.Engine) *HTTPClient
NewHTTPClient 创建一个新的HTTP测试客户端
func (*HTTPClient) DELETE ¶
func (c *HTTPClient) DELETE(path string, headers map[string]string) *Response
DELETE 发送DELETE请求
func (*HTTPClient) PATCH ¶
func (c *HTTPClient) PATCH(path string, body interface{}, headers map[string]string) *Response
PATCH 发送PATCH请求
func (*HTTPClient) POST ¶
func (c *HTTPClient) POST(path string, body interface{}, headers map[string]string) *Response
POST 发送POST请求
func (*HTTPClient) PUT ¶
func (c *HTTPClient) PUT(path string, body interface{}, headers map[string]string) *Response
PUT 发送PUT请求
func (*HTTPClient) UploadFile ¶
func (c *HTTPClient) UploadFile(path, fieldName, filePath string, additionalFields map[string]string) *Response
UploadFile 上传文件测试辅助函数
func (*HTTPClient) WithBaseURL ¶
func (c *HTTPClient) WithBaseURL(baseURL string) *HTTPClient
WithBaseURL 设置基础URL
func (*HTTPClient) WithHeader ¶
func (c *HTTPClient) WithHeader(key, value string) *HTTPClient
WithHeader 添加请求头
func (*HTTPClient) WithHeaders ¶
func (c *HTTPClient) WithHeaders(headers map[string]string) *HTTPClient
WithHeaders 批量添加请求头
type Helper ¶
Helper 测试助手结构
func (*Helper) AssertContainsAll ¶
AssertContainsAll 断言一个字符串包含所有指定的子字符串
func (*Helper) AssertNotContainsAny ¶
AssertNotContainsAny 断言一个字符串不包含任何指定的子字符串
func (*Helper) LoadFixtureFile ¶
LoadFixtureFile 加载测试数据文件
func (*Helper) RandomString ¶
RandomString 生成指定长度的随机字符串
func (*Helper) SetupAndTeardown ¶
func (h *Helper) SetupAndTeardown(setup func(), teardown func())
SetupAndTeardown 设置测试的前置和后置处理
func (*Helper) SetupTestDB ¶
SetupTestDB 设置测试数据库连接 注意:此函数仅为示例,实际实现需要进行适当修改 通常的实现方式为: 1. 创建临时数据库文件 2. 构建数据库连接字符串 3. 建立数据库连接 4. 根据需要进行数据库初始化
type IntegrationTest ¶
type IntegrationTest struct {
T *testing.T
App *flow.Engine
TestDB *gorm.DB
Helper *Helper
SQLiteDSN string
}
IntegrationTest 提供集成测试支持
func NewIntegrationTest ¶
func NewIntegrationTest(t *testing.T) *IntegrationTest
NewIntegrationTest 创建一个新的集成测试助手
func (*IntegrationTest) CreateFixturesDir ¶
func (i *IntegrationTest) CreateFixturesDir() string
CreateFixturesDir 创建测试数据文件目录
func (*IntegrationTest) CreateTestFixtures ¶
func (i *IntegrationTest) CreateTestFixtures(fixturePath string, modelType interface{})
CreateTestFixtures 从文件创建测试数据
func (*IntegrationTest) ExecuteSQL ¶
func (i *IntegrationTest) ExecuteSQL(query string, args ...interface{})
ExecuteSQL 执行原始SQL查询
func (*IntegrationTest) MigrateTestModels ¶
func (i *IntegrationTest) MigrateTestModels(models ...interface{})
MigrateTestModels 为测试模型创建数据库表
func (*IntegrationTest) NewHTTPTest ¶
func (i *IntegrationTest) NewHTTPTest() *HTTPClient
NewHTTPTest 创建用于HTTP测试的客户端
func (*IntegrationTest) RunWithTimeout ¶
func (i *IntegrationTest) RunWithTimeout(timeout time.Duration, fn func(ctx context.Context))
RunWithTimeout 在指定超时时间内运行测试函数
func (*IntegrationTest) SetupRepositoryWithTx ¶
func (i *IntegrationTest) SetupRepositoryWithTx(modelType interface{}) *db.GenericRepository[interface{}]
SetupRepositoryWithTx 使用事务设置仓储 这是一个通用示例,实际使用时需要根据项目的仓储实现进行调整
func (*IntegrationTest) SetupTestApp ¶
func (i *IntegrationTest) SetupTestApp() *flow.Engine
SetupTestApp 设置测试应用
func (*IntegrationTest) SetupTestDB ¶
func (i *IntegrationTest) SetupTestDB() *gorm.DB
SetupTestDB 设置测试数据库
func (*IntegrationTest) SetupTestTransaction ¶
func (i *IntegrationTest) SetupTestTransaction() *sql.Tx
SetupTestTransaction 开始一个测试事务
func (*IntegrationTest) TruncateTable ¶
func (i *IntegrationTest) TruncateTable(tableName string)
TruncateTable 清空数据表中的数据
type IntegrationTestSuite ¶
type IntegrationTestSuite interface {
SetupSuite(t *testing.T)
TeardownSuite()
SetupTest(t *testing.T)
TeardownTest()
}
IntegrationTestSuite 集成测试套件接口
type Mock ¶
type Mock struct {
Calls map[string][][]interface{}
}
Mock 是单元测试中的模拟对象基类
func (*Mock) RecordCall ¶
RecordCall 记录对模拟对象的方法调用
type MockAssertions ¶
MockAssertions 提供对模拟对象的断言
func NewMockAssertions ¶
func NewMockAssertions(t *testing.T, mock *Mock) *MockAssertions
NewMockAssertions 创建新的模拟断言助手
func (*MockAssertions) AssertCalled ¶
func (a *MockAssertions) AssertCalled(methodName string, args ...interface{}) bool
AssertCalled 断言方法已被调用
func (*MockAssertions) AssertCalledTimes ¶
func (a *MockAssertions) AssertCalledTimes(methodName string, times int) bool
AssertCalledTimes 断言方法被调用特定次数
func (*MockAssertions) AssertNotCalled ¶
func (a *MockAssertions) AssertNotCalled(methodName string) bool
AssertNotCalled 断言方法未被调用
type MockGenerator ¶
type MockGenerator struct {
// contains filtered or unexported fields
}
MockGenerator 提供用于测试的数据生成功能
func (*MockGenerator) FillStruct ¶
func (g *MockGenerator) FillStruct(s interface{})
FillStruct 使用随机数据填充结构体
func (*MockGenerator) RandomDate ¶
func (g *MockGenerator) RandomDate(startYear, endYear int) time.Time
RandomDate 生成指定范围内的随机日期
func (*MockGenerator) RandomElement ¶
func (g *MockGenerator) RandomElement(slice interface{}) interface{}
RandomElement 从切片中随机选择一个元素
func (*MockGenerator) RandomFloat ¶
func (g *MockGenerator) RandomFloat(min, max float64) float64
RandomFloat 生成指定范围内的随机浮点数
func (*MockGenerator) RandomInt ¶
func (g *MockGenerator) RandomInt(min, max int) int
RandomInt 生成指定范围内的随机整数
func (*MockGenerator) RandomPhoneNumber ¶
func (g *MockGenerator) RandomPhoneNumber() string
RandomPhoneNumber 生成随机手机号
func (*MockGenerator) RandomString ¶
func (g *MockGenerator) RandomString(length int) string
RandomString 生成指定长度的随机字符串
func (*MockGenerator) RandomSubset ¶
func (g *MockGenerator) RandomSubset(slice interface{}, n int) interface{}
RandomSubset 从切片中随机选择n个元素
func (*MockGenerator) WithSeed ¶
func (g *MockGenerator) WithSeed(seed int64) *MockGenerator
WithSeed 使用固定种子初始化随机数生成器,用于生成可重现的数据
type Request ¶
type Request struct {
Method string
Path string
Headers map[string]string
Body interface{}
Query url.Values
}
Request 定义HTTP请求结构
type Response ¶
type Response struct {
StatusCode int
Header http.Header
Body []byte
Result interface{}
Raw *httptest.ResponseRecorder
}
Response 定义HTTP响应结构
func (*Response) AssertHeader ¶
AssertHeader 断言Header等于指定值
func (*Response) AssertHeaderContains ¶
AssertHeaderContains 断言Header包含指定值
func (*Response) AssertJSON ¶
AssertJSON 断言JSON响应
func (*Response) AssertJSONContains ¶
AssertJSONContains 断言JSON响应中包含指定字段
func (*Response) AssertStatus ¶
AssertStatus 断言HTTP状态码
type UnitTest ¶
UnitTest 提供单元测试支持
func (*UnitTest) AssertJSONEqual ¶
AssertJSONEqual 断言两个JSON字符串相等
func (*UnitTest) CallMethod ¶
func (u *UnitTest) CallMethod(obj interface{}, methodName string, args ...interface{}) []interface{}
CallMethod 调用对象的方法并返回结果
func (*UnitTest) GetFunctionName ¶
GetFunctionName 获取函数的名称(用于测试函数名称)
func (*UnitTest) RunTestCase ¶
RunTestCase 运行单个测试用例
func (*UnitTest) RunTestCases ¶
RunTestCases 运行多个测试用例