Introduction
The gtest
module offers simplified, lightweight, and commonly used unit testing methods. It is an extension encapsulated on the standard library testing
, mainly adding the following features:
- Isolation of multiple test items within unit test cases.
- Addition of a series of commonly used test assertion methods.
- Assertion methods support various common format assertions, increasing usability.
- Unified formatting of error messages when test failures occur.
gtest
is designed to be simple and easy to use, capable of meeting the vast majority of unit testing scenarios. For more complex testing scenarios, consider third-party testing frameworks such as testify
and goconvey
.
Usage:
import "github.com/gogf/gf/v2/test/gtest"
API Documentation:
This chapter may not be updated in a timely manner; for a more comprehensive introduction to the API, please refer to the API documentation.
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{})
Brief Explanation:
- Use the
C
method to create aCase
, representing a unit test case. A unit test method can contain multipleC
, where eachC
often represents one of the possible tests of the method. - The assertion method
Assert
supports comparison of variables of any type. When performing assertion comparison withAssertEQ
, it also compares types, i.e., it performs strict assertions. - When using size comparison assertion methods such as
AssertGE
, the parameters support both string and number comparison, where string comparison is case-sensitive. - Inclusion assertion methods
AssertIN
andAssertNI
support parameters of theslice
type but do not currently supportmap
type parameters.
The package name for unit testing can either be package_name_test
or directly use package_name
(the same name as the test package). Both approaches are quite common and are also both involved in the Go official standard library. However, it should be noted that when you need to test private methods/private variables of the package, you must use the package_name
naming form. Also, when using the package_name
naming method, ensure that methods related to unit testing (non-Test*
test methods) are generally defined as private and not publicly exposed.
Usage Example
For example, one of the unit test cases of the gstr
module:
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")
})
}
It can also be used like this:
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")
})
}
A unit test case can contain multiple C
, and a C
can perform multiple assertions. If assertions pass, it directly proceeds with a PASS; however, if assertions fail, the following error information is output, and the current unit test case's execution is terminated (subsequent other unit test cases will not be terminated).
=== 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