一:单元测试

1.为什么要做单元测试和性能测试

  • 减少bug
  • 快速定位bug
  • 减少调试时间
  • 提高代码质量

2.golang的单元测试

  • 单元测试代码的go文件必须以_test.go结尾
  • 单元测试的函数名必须以Test开头,是可导出公开的函数
  • 测试函数的签名必须接收一个指向testing.T类型的指针,并且不能返回任何值

3.golang单元测试组

  • 有好几个不同的输入以及输出组成的一组单元测试

4.测试覆盖率

go test -covergo test -run TestAllgo test -cover -coverprofile=c.outgo tool cover -html=c.out -o coverage.html

二:性能测试

go test -bench=".*"go test -bench="BenchmarkWithPool"go test -bench="BenchmarkWithPool" -cpuprofile cpu.out -memprofile mem.outgo tool pprof cpu.outgo tool pprof mem.out