golang测试快速上手【表格驱动、覆盖率、性能、http、gin测试】

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

1. 表格驱动测试

  • 测试数据与逻辑分离

  • 出错信息可以自己diy【而不是简单的像Java那样期望得到,实际得到】

  • 跑完所有的测试数据才会停

    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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值