單元測試&基準測試&樣本測試&測試覆蓋率

1.單元測試

1.1.go test 目錄

go test,將對當前目錄下的所有*_test.go文件進行編譯并自動運行測試
go test xxx 將對指定目錄下的所有*_test.go文件進行編譯并自動運行測試

1.2.go test 測試源碼文件 測試的源碼文件

運行test目錄下t.go文件的所有測試用例
go test test/t_test.og test/t.go

1.3.go test -run=正則表達式與之對應的測試用例執行 目錄名稱,執行./test目錄下正則匹配的所有單元測試

go test -v -run=Test ./test

1.4.go test -v -run=T ./test -timeout 100ms timeout指定單元測試的超時時間,超過指定時間測試用通不通過

 go test -v -run=T ./test -timeout 100m

1.5.go test 并發測試

go test -v ./test -parallel=5
運行條件
1.功能測試函數加入t.Parallel()
2.runtime.GOMAXPROCS設置go的最大并發處理數量,默認值為1
3.測試用例源碼文件init中設置runtime.GOMAXPROCS最大并發數據
4.通常-parallel不需要設置,默認讓它與runtime.GOMAXPROCS數據相同
  • t.go
package test

import (
    "time"
    "fmt"
    "strconv"
)

func test()  {
    //fmt.Println("test")
    time.Sleep(time.Nanosecond)
}

func test2()  {
    i := 1
    name := "jingyanlei"
    age := 18
    fmt.Sprintf("i:%d, name:%s, age:%d", i, name, age)
}

func test3()  {
    i := 1
    name := "jingyanlei"
    age := 18
    _ = "i:"+strconv.Itoa(i)+"name:"+name+"age:"+strconv.Itoa(age)
}
  • t_test.go
package test

import (
    "testing"
    "runtime"
)

func init() {
    runtime.GOMAXPROCS(10)
}

func TestTest(t *testing.T)  {
    t.Parallel()
    test()
}

func TestTest2(t *testing.T) {
    t.Parallel()
    test2()
}

2.基準測試

2.1.運行基準測試

運行指定的基準測試
go test -bench regexp
go test -bench=Test ./test/ 

運行所有基準測試
go test -bench. 或go test -bench=".*" 

對某個go文件進行benchmark測試 
go test ./test/t_test.go -=".*"

-benchmem 基準測試中包含內存分配信息
go test  -bench=Test ./test/  -benchmem 

-benchtime t 用來間接控制基準測試函數的操作次數 t時間 1s 1秒
go test  -bench=Test ./test/  -benchmem -benchtime 1s 

-cpu 自定義測試運行次數,并在測試運行時間改變go語言的最大并發處理數的標記
go test  -bench=Test ./test/  -benchmem -benchtime 1s -cpu=1,4,16

b.SetBytes(1024) MB/s 每秒被處理的字節數量 
ns/op 平均耗時
B/op 每次操作分配的字節平均數
allocs/op 每次操作分配內存的次數

3.樣本測試

樣本測試函數需要以"Example"作為開始,并且在這類的函數體的最后還可以有若干個注釋行,作用比較在該測試函數被執行期間,標準輸出上出現的內容是否與預期相符
  • 命名
按照慣例,根據被測試的程序實體的種類,應該遵循這樣的命名規則
1.當被測試對象是整個代碼包時,樣本測試的函數名稱應該是Example,即直接以樣本測試函數名的統一前輟作為函數名稱
2.當被測試對象是一個函數時,對于函數F,樣本測試的名稱應該是ExampleF
3.當被測試對象是一個類型時,對于類型T,樣本測試函數的名稱應該是ExampleT
4.當被測試對象是某個類型中的一個方法時,對于類型T的方法M,樣本測試測試函數的名稱應該是ExampleT_M
5.如果需要在樣本測試的名稱后添加上后輟,需要用下劃線把該后輟與名稱其它部分隔開,該后輟的首字母必須小寫,如ExampleT_M_basic
  • 執行
go test test/et
  • Example
package et

import "fmt"

func ExampleHello()  {
    fmt.Println("Hello,Golang~")
    //Output:Hello,Golang~
}

func ExampleHello2()  {
    for i:=0; i<3; i++  {
        fmt.Println("Hello,Golang~")
    }

    //Output:Hello,Golang~
    //Hello,Golang~
    //Hello,Golang~
}

4.測試運行記錄

  • 收集資源使用情況
在測試運行時資源的使用情況監控方面,go語言提供了3個可用的標記。只能運行在一個代碼包下。
標記名稱 標記描述
-cpuprofile cpu.out 記錄cpu使用情況,記寫到指定文件中直到測試退出
-memprofile mem.out 記錄內存使有情況,并在所有測試通過后將內存使用概要寫入到指定的測試文件中
-memprofilerate 此標記記錄內存分配操作的行為,設置分析器的取樣時間間隔,這些記錄將會被寫到內存使用概要文件中
go test ./test -cpuprofile cpu.out
目錄下會生成cpu.out文件
使用go tool pprof命令來交互式的對這個概要文件進行查看
go tool pprof ./test.test ./cpu.out
(pprof) top10
go test ./test -memprofile mem.out -memprofilerate 10
查看
go tool pprof ./test.test ./mem.out
(pprof) top10
2319B of 2319B total (  100%)
Dropped 38 nodes (cum <= 11B)
Showing top 10 nodes out of 26 (cum >= 95B)
      flat  flat%   sum%        cum   cum%
     1664B 71.76% 71.76%      1664B 71.76%  runtime.malg
      240B 10.35% 82.10%       240B 10.35%  runtime.acquireSudog
      192B  8.28% 90.38%      1984B 85.55%  runtime.systemstack
      128B  5.52% 95.90%       128B  5.52%  runtime.allgadd
       48B  2.07% 97.97%        48B  2.07%  runtime.rawstringtmp
       47B  2.03%   100%        47B  2.03%  os.NewFile
         0     0%   100%        48B  2.07%  fmt.Sprintf
         0     0%   100%        80B  3.45%  go-test/test.TestTest
         0     0%   100%        80B  3.45%  go-test/test.TestTest2
         0     0%   100%        95B  4.10%  main.main

  • 記錄程序中GOroutine阻塞事件
標記名稱 標記描述
-blockprofile block.out 記錄Goroutine阻塞事件,并在所有測試通過后將概要信息寫到指定文件中.
-blickprofilerate n 這個標記用于控制記錄Goroutine阻塞事件的時間間隔,n的單位為次,默認值為1
go test ./test -blockprofile block.out -blockprofilerate 1 

查看
go tool pprof ./test.test ./block.out 
(pprof) top10
312.26us of 312.26us total (  100%)
Showing top 10 nodes out of 15 (cum >= 4.84us)
      flat  flat%   sum%        cum   cum%
  296.58us 94.98% 94.98%   296.58us 94.98%  runtime.chanrecv1
   15.69us  5.02%   100%    15.69us  5.02%  runtime.chansend1
         0     0%   100%    41.67us 13.34%  go-test/test.TestTest
         0     0%   100%    10.10us  3.23%  go-test/test.TestTest2
         0     0%   100%   123.37us 39.51%  main.main
         0     0%   100%   312.26us   100%  runtime.goexit
         0     0%   100%   123.37us 39.51%  runtime.main
         0     0%   100%   123.37us 39.51%  testing.(*M).Run
         0     0%   100%    51.76us 16.58%  testing.(*T).Parallel
         0     0%   100%     4.84us  1.55%  testing.(*T).Run

5.測試覆蓋率

go test cover
...
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • Spring Cloud為開發人員提供了快速構建分布式系統中一些常見模式的工具(例如配置管理,服務發現,斷路器,智...
    卡卡羅2017閱讀 134,868評論 18 139
  • 轉:http://www.lxweimin.com/p/d5fca0185e83 Xcode測試 前言 總算在今天把...
    測試小螞蟻閱讀 2,988評論 0 20
  • 1. Java基礎部分 基礎部分的順序:基本語法,類相關的語法,內部類的語法,繼承相關的語法,異常的語法,線程的語...
    子非魚_t_閱讀 31,754評論 18 399
  • 作者:廖飛全文約 7778 字,讀完可能需要 15 分鐘。 原文鏈接:http://www.cnblogs.com...
    羅義的夏天閱讀 4,585評論 1 2
  • 一年前母親悄無聲息的走了,看到兒孫們都生活的很幸福,為了不拖累兒女們,她悄悄的走了。 十幾年前父親就因...
    hesiting閱讀 287評論 4 3