导航菜单

Go语言测试与性能优化

单元测试

Go内置testing包,支持简单高效的单元测试。

package mathutil

import "testing"

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

func TestAdd(t *testing.T) {
    if Add(2, 3) != 5 {
        t.Error("Add(2,3) 应为5")
    }
}
  • 测试文件以_test.go结尾
  • 测试函数以Test开头
  • go test命令运行