gtest
模块提供了简便化的、轻量级的、常用的单元测试方法。是基于标准库testing
的功能扩展封装,主要增加实现了以下特性:
|
使用方式:
import "github.com/gogf/gf/v2/test/gtest" |
接口文档:
https://pkg.go.dev/github.com/gogf/gf/v2/test/gtest
func C(t *testing.T, f func(t *T)) func Assert(value, expect interface{}) func AssertEQ(value, expect interface{}) func AssertGE(value, expect interface{}) func AssertGT(value, expect interface{}) func AssertIN(value, expect interface{}) func AssertLE(value, expect interface{}) func AssertLT(value, expect interface{}) func AssertNE(value, expect interface{}) func AssertNI(value, expect interface{}) func Error(message ...interface{}) func Fatal(message ...interface{}) |
简要说明:
C
方法创建一个Case
,表示一个单元测试用例。一个单元测试方法可以包含多个C
,每一个C
包含的用例往往表示该方法的其中一种可能性测试。Assert
支持任意类型的变量比较。AssertEQ
进行断言比较时,会同时比较类型,即严格断言。AssertGE
时,参数支持字符串及数字比较,其中字符串比较为大小写敏感。AssertIN
及AssertNI
支持slice
类型参数,暂不支持map
类型参数。用于单元测试的包名既可以使用 |
使用示例:
例如gstr
模块其中一个单元测试用例:
package gstr_test import ( "github.com/gogf/gf/v2/test/gtest" "github.com/gogf/gf/v2/text/gstr" "testing" ) func Test_Trim(t *testing.T) { gtest.C(t, func(t *gtest.T) { t.Assert(gstr.Trim(" 123456\n "), "123456") t.Assert(gstr.Trim("#123456#;", "#;"), "123456") }) } |
也可以这样使用:
package gstr_test import ( . "github.com/gogf/gf/v2/test/gtest" "github.com/gogf/gf/v2/text/gstr" "testing" ) func Test_Trim(t *testing.T) { C(t, func() { Assert(gstr.Trim(" 123456\n "), "123456") Assert(gstr.Trim("#123456#;", "#;"), "123456") }) } |
一个单元测试用例可以包含多个C
,一个C
也可以执行多个断言。 断言成功时直接PASS,但是如果断言失败,会输出如下类似的错误信息,并终止当前单元测试用例的继续执行(不会终止后续的其他单元测试用例)。
=== RUN Test_Trim [ASSERT] EXPECT 123456#; == 123456 1. /Users/john/Workspace/Go/GOPATH/src/github.com/gogf/gf/v2/text/gstr/gstr_z_unit_trim_test.go:20 2. /Users/john/Workspace/Go/GOPATH/src/github.com/gogf/gf/v2/text/gstr/gstr_z_unit_trim_test.go:18 --- FAIL: Test_Trim (0.00s) FAIL |