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.

Unit Testing - 图1tip

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

  1. 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

  1. func C(t *testing.T, f func(t *T))
  2. func Assert(value, expect interface{})
  3. func AssertEQ(value, expect interface{})
  4. func AssertGE(value, expect interface{})
  5. func AssertGT(value, expect interface{})
  6. func AssertIN(value, expect interface{})
  7. func AssertLE(value, expect interface{})
  8. func AssertLT(value, expect interface{})
  9. func AssertNE(value, expect interface{})
  10. func AssertNI(value, expect interface{})
  11. func Error(message ...interface{})
  12. func Fatal(message ...interface{})

Brief Explanation

  1. Use the C method to create a Case, representing a unit test case. A unit test method can contain multiple C, where each C often represents one of the possible tests of the method.
  2. The assertion method Assert supports comparison of variables of any type. When performing assertion comparison with AssertEQ, it also compares types, i.e., it performs strict assertions.
  3. When using size comparison assertion methods such as AssertGE, the parameters support both string and number comparison, where string comparison is case-sensitive.
  4. Inclusion assertion methods AssertIN and AssertNI support parameters of the slice type but do not currently support map 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:

  1. package gstr_test
  2. import (
  3. "github.com/gogf/gf/v2/test/gtest"
  4. "github.com/gogf/gf/v2/text/gstr"
  5. "testing"
  6. )
  7. func Test_Trim(t *testing.T) {
  8. gtest.C(t, func(t *gtest.T) {
  9. t.Assert(gstr.Trim(" 123456\n "), "123456")
  10. t.Assert(gstr.Trim("#123456#;", "#;"), "123456")
  11. })
  12. }

It can also be used like this:

  1. package gstr_test
  2. import (
  3. . "github.com/gogf/gf/v2/test/gtest"
  4. "github.com/gogf/gf/v2/text/gstr"
  5. "testing"
  6. )
  7. func Test_Trim(t *testing.T) {
  8. C(t, func() {
  9. Assert(gstr.Trim(" 123456\n "), "123456")
  10. Assert(gstr.Trim("#123456#;", "#;"), "123456")
  11. })
  12. }

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).

  1. === RUN Test_Trim
  2. [ASSERT] EXPECT 123456#; == 123456
  3. 1. /Users/john/Workspace/Go/GOPATH/src/github.com/gogf/gf/v2/text/gstr/gstr_z_unit_trim_test.go:20
  4. 2. /Users/john/Workspace/Go/GOPATH/src/github.com/gogf/gf/v2/text/gstr/gstr_z_unit_trim_test.go:18
  5. --- FAIL: Test_Trim (0.00s)
  6. FAIL