使用Go進行單元測試的實現
阿新 • • 發佈:2020-01-09
簡介
日常開發中,測試是不能缺少的.
Go 標準庫中有一個叫做 testing 的測試框架,可以用於單元測試和效能測試.
它是和命令 go test 整合使用的.
測試檔案是以後綴 _test.go 命名的,通常和被測試的檔案放在同一個包中.
單元測試
單元測試的格式形如:
func TestAbs(t *testing.T) { got := Abs(-1) if got != 1 { t.Errorf("Abs(-1) = %d; want 1",got) } }
在 util 目錄下建立一個檔案 util_test.go,新增一個單元測試:
package util import "testing" // 普通的測試 func TestGenShortID(t *testing.T) { shortID,err := GenShortID() if shortID == "" || err != nil { t.Error("GenShortID failed") } }
然後,在根目錄下執行 go test -v ./util/,測試結果如下:
root@592402321ce7:/workspace# go test -v ./util/ === RUN TestGenShortID --- PASS: TestGenShortID (0.00s) PASS ok tzh.com/web/util 0.006s
效能測試
效能測試的結果形如:
func BenchmarkHello(b *testing.B) { for i := 0; i < b.N; i++ { fmt.Sprintf("hello") } }
在 util_test.go 新增效能測試:
// 效能測試 func BenchmarkGenShortID(b *testing.B) { for i := 0; i < b.N; i++ { GenShortID() } }
執行結果如下(使用 --run=none 避免執行普通的測試函式,因為一般不可能有函式名匹配 none):
root@592402321ce7:/workspace# go test -v -bench="BenchmarkGenShortID$" --run=none ./util/ goos: linux goarch: amd64 pkg: tzh.com/web/util BenchmarkGenShortID-2 507237 2352 ns/op PASS ok tzh.com/web/util 1.229s
這說明,平均每次執行 GenShortID() 需要 2352 納秒.
效能分析
執行測試的時候,可以指定一些引數,生成效能檔案 profile.
-blockprofile block.out Write a goroutine blocking profile to the specified file when all tests are complete. Writes test binary as -c would. -blockprofilerate n Control the detail provided in goroutine blocking profiles by calling runtime.SetBlockProfileRate with n. See 'go doc runtime.SetBlockProfileRate'. The profiler aims to sample,on average,one blocking event every n nanoseconds the program spends blocked. By default,if -test.blockprofile is set without this flag,all blocking events are recorded,equivalent to -test.blockprofilerate=1. -coverprofile cover.out Write a coverage profile to the file after all tests have passed. Sets -cover. -cpuprofile cpu.out Write a CPU profile to the specified file before exiting. Writes test binary as -c would. -memprofile mem.out Write an allocation profile to the file after all tests have passed. Writes test binary as -c would. -memprofilerate n Enable more precise (and expensive) memory allocation profiles by setting runtime.MemProfileRate. See 'go doc runtime.MemProfileRate'. To profile all memory allocations,use -test.memprofilerate=1. -mutexprofile mutex.out Write a mutex contention profile to the specified file when all tests are complete. Writes test binary as -c would. -mutexprofilefraction n Sample 1 in n stack traces of goroutines holding a contended mutex.
使用下面的命令,生成 CPU 的 profile:
go test -v -bench="BenchmarkGenShortID$" --run=none -cpuprofile cpu.out ./util/
當前目錄下,應該會生成 cpu.out 檔案和 util.test 檔案.
使用下面的命令,觀察耗時操作:
# 進入互動模式 go tool pprof cpu.out top
安裝 Graphviz 後可以生成視覺化的分析圖.
apt install graphviz go tool pprof -http=":" cpu.out
測試覆蓋率
root@592402321ce7:/workspace# go test -v -coverprofile=cover.out ./util/ === RUN TestGenShortID --- PASS: TestGenShortID (0.00s) PASS coverage: 9.1% of statements ok tzh.com/web/util 0.005s coverage: 9.1% of statements root@592402321ce7:/workspace# go tool cover -func=cover.out tzh.com/web/util/util.go:12: GenShortID 100.0% tzh.com/web/util/util.go:17: GetReqID 0.0% tzh.com/web/util/util.go:22: TimeToStr 0.0% tzh.com/web/util/util.go:30: GetTag 0.0% total: (statements) 9.1%
使用 -coverprofile=cover.out 選項可以統計測試覆蓋率.使用 go tool cover -func=cover.out 可以檢視更加詳細的測試覆蓋率結果,
統計每個函式的測試覆蓋率.
總結
測試是開發中非常重要的一個環節,用於保證軟體質量,切不可偷懶.
當前部分的程式碼
作為版本 v0.15.0
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。