https://juejin.cn/post/6970615934255906830
https://geektutu.com/post/hpg-benchmark.html

1. 表格驱动测试

func TestGoT(t *testing.T) {
	tests := []struct {
		name string
		in1 string
		in2 int
		res string
	}{
		{"test1", "biturd", 1, "biturd1"},
		{"test2", "biturd", 2, "biturd2"},
		{"test3", "biturd", 3, "biturd3"},
	}
	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {
			// 可以用参数调用预期方法并比较
			if !(test.in1 + strconv.Itoa(test.in2) == test.res){
				t.Errorf("%s run failed with the param (%v %v) for res (%v)",
					test.name, test.in1, test.in2, test.res)
			}
		})
	}
}

2. 覆盖率、性能测试

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-IOv1ZSUs-1627920563376)(image-20210802234930849.png)]

覆盖率测试、性能测试

覆盖率测试

go test -coverprofile=test.out
go tool cover -html=c.out

性能测试

go test -bench .
# 分析
go test -bench . -cpuprofile cpu-analyze.out
go tool pprof  cpu-analyze.out
选择web

耗时比例、详情树形图
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-HP6R45r1-1627920563387)(image-20210802235435510.png)]

func TestFibonacci(t *testing.T) {

   //预先定义的一组斐波那契数列作为测试用例
   fsMap := map[int]int{}
   fsMap[0] = 0
   fsMap[1] = 1
   fsMap[2] = 1
   fsMap[3] = 2
   fsMap[4] = 3
   fsMap[5] = 5
   fsMap[6] = 8
   fsMap[7] = 13
   fsMap[8] = 21
   fsMap[9] = 34

   for k, v := range fsMap {
      fib := Fibonacci(k)
      if v == fib {
         t.Logf("结果正确:n为%d,值为%d", k, fib)
      } else {
         t.Errorf("结果错误:期望%d,但是计算的值是%d", v, fib)
      }
   }
}
// 内存统计
func BenchmarkFibonacci(b *testing.B) {

   n := 10

   b.ReportAllocs() //开启内存统计
   b.ResetTimer() //【准备好输入数据刷新计时器】

   for i := 0; i < b.N; i++ {
      Fibonacci(n)
   }
}

// 并发度测试
func BenchmarkFibonacciRunParallel(b *testing.B) {
   n := 10
   b.RunParallel(func(pb *testing.PB) {
      for pb.Next() {
         Fibonacci(n)
      }
   })
}

3. Http测试【gin、httprouter】

package controller

import (
   "bytes"
   "encoding/json"
   "github.com/gin-gonic/gin"
   "github.com/magiconair/properties/assert"
   "io/ioutil"
   "net/http"
   "net/http/httptest"
   "testing"
)


func TestRemoveCdnImage(t *testing.T) {
   // 定义路由
   router := gin.Default()
   router.POST("/test", RemoveCdnImage)

   tests := []struct {
      name string
      expect string
   }{
      {
         name: "test api",
         expect: "ok",
      },
   }
   for _, tt := range tests {
      t.Run(tt.name, func(t *testing.T) {
         params := struct{
            params string
         }{
            params: "paramsBody",
         }

         paramsByte, _ := json.Marshal(params)
         w := httptest.NewRecorder()
         req := httptest.NewRequest("POST", "/test", bytes.NewBuffer(paramsByte))
         router.ServeHTTP(w, req)

         assert.Equal(t, http.StatusOK, w.Code)
         res, _ := ioutil.ReadAll(w.Body)
         var ret string
         if err := json.Unmarshal(res, &ret); err!=nil {
            t.Error(err)
         }
         assert.Equal(t, tt.expect, ret)

      })
   }
}

https://learnku.com/articles/52896

https://www.jianshu.com/p/41cdfd4a5707

https://gobea.cn/blog/detail/yolK1W5P.html

https://segmentfault.com/a/1190000019568490