一. 基础介绍

1. 创建测试文件

  • 测试文件通常与要测试的代码文件位于同一个包中。
  • 测试文件的名称应该以 _test.go 结尾。例如,如果你要测试的文件是 math.go,那么测试文件可以命名为 math_test.go

2. 编写测试函数

  • 测试函数必须导入 testing 包。
  • 每个测试函数必须以 Test 开头,后跟一个首字母大写的名字,例如 TestSumTestSubtract
  • 测试函数的签名应该接受一个指向 testing.T 类型的指针:func TestXxx(t *testing.T) { ... }

3. 使用 t 对象进行断言和日志记录

  • t 对象用于记录测试信息和控制测试流程。
  • 使用 t.Errort.Errorf 报告失败,但继续执行当前测试。
  • 使用 t.Fatalt.Fatalf 报告失败并立即终止当前测试。

4. 运行测试

  • 在命令行中,进入包含测试文件的目录。
  • 执行 go test 命令运行所有测试,或使用 go test -v 以详细模式运行(打印每个测试的名字和运行状态)。
  • 使用 go test -run 加上正则表达式来运行特定的测试。例如,go test -run TestSum 仅运行名为 TestSum 的测试。

示例

假设有一个名为 math.go 的文件,其中定义了一个函数 Sum

1
2
3
4
5
6
goCopy code// math.go
package math

func Sum(a, b int) int {
return a + b
}

创建一个名为 math_test.go 的测试文件,其中包含以下内容:

1
2
3
4
5
6
7
8
9
10
11
goCopy code// math_test.go
package math

import "testing"

func TestSum(t *testing.T) {
total := Sum(5, 5)
if total != 10 {
t.Errorf("Sum was incorrect, got: %d, want: %d.", total, 10)
}
}

然后在终端中运行 go testgo test -v 来执行测试。

二. 综合案例

  1. 结构

    image-20240115233924014

  2. monster.go

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    package test_case

    import (
    "encoding/json"
    "fmt"
    "os"
    )

    type Monster struct {
    Name string `json:"name"`
    Age int `json:"age"`
    Skill string `json:"skill"`
    }

    // Store 将其序列化保存为文件
    func (m *Monster) Store() bool {
    data, err := json.Marshal(m)
    if err != nil {
    fmt.Println("json parse Monster err ", err)
    return false
    }
    // 写入文件
    err = os.WriteFile("d:/monster.txt", data, 0666)
    if err != nil {
    fmt.Println("write file err ", err)
    return false
    }
    return true
    }

    // ReStore 反序列化文件
    func (m *Monster) ReStore() bool {
    data, err := os.ReadFile("d:/monster.txt")
    if err != nil {
    fmt.Println("read file err ", err)
    return false
    }
    // 将读取的数据进行反序列化
    err = json.Unmarshal(data, m)
    if err != nil {
    fmt.Println("json Unmarshal err ", err)
    return false
    }
    return true
    }

  3. monster_test.go

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    package test_case

    import (
    "testing"
    )

    func TestStore(t *testing.T) {
    monster := &Monster{
    Name: "小狐狸",
    Age: 200,
    Skill: "魅惑",
    }
    res := monster.Store()
    if !res {
    t.Fatalf("TestStore fail,expected is %v,but got %v", true, res)
    }
    t.Logf("TestStore 测试通过")

    }

    func TestReStore(t *testing.T) {
    monster := &Monster{} //空的结构体

    res := monster.ReStore() // 序列化后结构体就有数据
    if !res {
    t.Fatalf("TestReStore fail,expected is %v,but got %v", true, res)
    }

    if monster.Name != "小狐狸" {
    t.Fatalf("TestStore fail,expected monster.Name is %v,but got %v", "小狐狸", monster.Name)
    }
    t.Logf("TestStore 测试通过")
    }

    image-20240115234203156