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=正則表達式與之對應的測試用例執(zhí)行 目錄名稱立哑,執(zhí)行./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 并發(fā)測試
go test -v ./test -parallel=5
運行條件
1.功能測試函數加入t.Parallel()
2.runtime.GOMAXPROCS設置go的最大并發(fā)處理數量,默認值為1
3.測試用例源碼文件init中設置runtime.GOMAXPROCS最大并發(fā)數據
4.通常-parallel不需要設置惦费,默認讓它與runtime.GOMAXPROCS數據相同
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)
}
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語言的最大并發(fā)處理數的標記
go test -bench=Test ./test/ -benchmem -benchtime 1s -cpu=1,4,16
b.SetBytes(1024) MB/s 每秒被處理的字節(jié)數量
ns/op 平均耗時
B/op 每次操作分配的字節(jié)平均數
allocs/op 每次操作分配內存的次數
3.樣本測試
樣本測試函數需要以"Example"作為開始南蹂,并且在這類的函數體的最后還可以有若干個注釋行,作用比較在該測試函數被執(zhí)行期間局蚀,標準輸出上出現的內容是否與預期相符
按照慣例麦锯,根據被測試的程序實體的種類,應該遵循這樣的命名規(guī)則
1.當被測試對象是整個代碼包時琅绅,樣本測試的函數名稱應該是Example,即直接以樣本測試函數名的統(tǒng)一前輟作為函數名稱
2.當被測試對象是一個函數時扶欣,對于函數F,樣本測試的名稱應該是ExampleF
3.當被測試對象是一個類型時,對于類型T千扶,樣本測試函數的名稱應該是ExampleT
4.當被測試對象是某個類型中的一個方法時料祠,對于類型T的方法M,樣本測試測試函數的名稱應該是ExampleT_M
5.如果需要在樣本測試的名稱后添加上后輟澎羞,需要用下劃線把該后輟與名稱其它部分隔開髓绽,該后輟的首字母必須小寫,如ExampleT_M_basic
go test test/et
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.測試運行記錄
在測試運行時資源的使用情況監(jiān)控方面妆绞,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
標記名稱 |
標記描述 |
-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
...