咱們能夠爲Go程序編寫三類測試,即:功能測試(test)、基準測試(benchmark),也稱性能測試(example)shell
package demo import ( "fmt" "testing" ) //功能測試 func TestMethod(t *testing.T) { fmt.Println("function test") } //基準測試 func BenchmarkMethod(b *testing.B) { fmt.Println("benchmark test") } //性能測試 func Examplemethod(t *testing.T) { fmt.Println("example test") } func Method(t *testing.T) { fmt.Println("Method") }
go test 命令,會自動讀取源碼目錄下面名爲 *_test.go 的文件,生成並運行測試用的可執行文件。windows
運行go test 命令會運行符合規定的測試代碼;緩存
go test命令就會針對每一個被測代碼包,依次地進行構建、執行包中符合要求的測試函數,清理臨時文件,打印測試結果。函數
命令:性能
go test demo #go test [目錄]
這裏是否讀緩存不會影響咱們的測試結果,若是想清初緩存能夠執行: go clean -cache測試
#格式:go test -bench=. -run=^$ [目錄] go test -bench=. -run=^$ demo
-bench= 指要進行性能測試code
. 須要執行任意名稱的性能測試函數(符合規則)blog
-run=^$ 須要執行哪些功能的測試函數源碼
^$ 只執行名稱爲空的功能測試函數(不執行任何功能函數)io
輸出的內容:
function test benchmark test goos: windows goarch: amd64 pkg: demo BenchmarkMethod-8 benchmark test benchmark test benchmark test benchmark test benchmark test 2000000000 0.00 ns/op PASS ok demo 0.262s
**** 碼字不易若是對你有幫助請給個關注****
**** 愛技術愛生活 QQ羣: 894109590****