- 最近在學go的api,但是我覺得我不能學個api把整個項目build一次,特地去搜了一把Go的單元測試
- 單元測試規則
- 每個測試文件必須以 _test.go 結尾,不然 go test 不能發現測試文件
- 每個測試文件必須導入 testing 包
- 功能測試函數必須以 Test 開頭,然后一般接測試函數的名字,這個不強求
- 測試方法參數必須 t *testing.T
- 性能測試case以Benchmark開頭
- 例子
- 文件結構
src └── api ├── compress_file.go └── compress_file_test.go
- go文件
package api import ( "bytes" "archive/tar" "log" "io" "fmt" "os" ) func Tar() { buf := new(bytes.Buffer) // Create a new tar archive. tw := tar.NewWriter(buf) // Add some files to the archive. var files = []struct { Name, Body string }{ {"readme.txt", "This archive contains some text files."}, {"gopher.txt", "Gopher names:\nGeorge\nGeoffrey\nGonzo"}, {"todo.txt", "Get animal handling licence."}, } for _, file := range files { hdr := &tar.Header{ Name: file.Name, Size: int64(len(file.Body)), } if err := tw.WriteHeader(hdr); err != nil { log.Fatalln(err) } if _, err := tw.Write([]byte(file.Body)); err != nil { log.Fatalln(err) } } // Make sure to check the error on Close. if err := tw.Close(); err != nil { log.Fatalln(err) } // Open the tar archive for reading. r := bytes.NewReader(buf.Bytes()) tr := tar.NewReader(r) // Iterate through the files in the archive. for { hdr, err := tr.Next() if err == io.EOF { // end of tar archive break } if err != nil { log.Fatalln(err) } fmt.Printf("Contents of %s:\n", hdr.Name) if _, err := io.Copy(os.Stdout, tr); err != nil { log.Fatalln(err) } fmt.Println() } }
- go 測試文件
package api import "testing" func TestTar(t *testing.T) { Tar(); }
- 測試方法
go test compress_file*.go -v -cover === RUN TestTar Contents of readme.txt: This archive contains some text files. Contents of gopher.txt: Gopher names: George Geoffrey Gonzo Contents of todo.txt: Get animal handling licence. --- PASS: TestTar (0.00s) PASS coverage: 78.3% of statements ok command-line-arguments 0.007s
- 關于go test命令
go test [-c] [-i] [build flags] [packages] [flags for test binary]
- 參數解讀
-c : 編譯go test成為可執行的二進制文件,但是不運行測試。 -i : 安裝測試包依賴的package,但是不運行測試。 關于build flags,調用go help build,這些是編譯運行過程中需要使用到的參數,一般設置為空 關于packages,調用go help packages,這些是關于包的管理,一般設置為空 關于flags for test binary,調用go help testflag,這些是go test過程中經常使用到的參數 -test.v : 是否輸出全部的單元測試用例(不管成功或者失敗),默認沒有加上,所以只輸出失敗的單元測試用例。 -test.run pattern: 只跑哪些單元測試用例 -test.bench patten: 只跑那些性能測試用例 -test.benchmem : 是否在性能測試的時候輸出內存情況 -test.benchtime t : 性能測試運行的時間,默認是1s -test.cpuprofile cpu.out : 是否輸出cpu性能分析文件 -test.memprofile mem.out : 是否輸出內存性能分析文件 -test.blockprofile block.out : 是否輸出內部goroutine阻塞的性能分析文件 -test.memprofilerate n : 內存性能分析的時候有一個分配了多少的時候才打點記錄的問題。這個參數就是設置打點的內存分配間隔,也就是profile中一個sample代表的內存大小。默認是設置為512 * 1024的。如果你將它設置為1,則每分配一個內存塊就會在profile中有個打點,那么生成的profile的sample就會非常多。如果你設置為0,那就是不做打點了。 你可以通過設置memprofilerate=1和GOGC=off來關閉內存回收,并且對每個內存塊的分配進行觀察。 -test.blockprofilerate n: 基本同上,控制的是goroutine阻塞時候打點的納秒數。默認不設置就相當于-test.blockprofilerate=1,每一納秒都打點記錄一下 -test.parallel n : 性能測試的程序并行cpu數,默認等于GOMAXPROCS。 -test.timeout t : 如果測試用例運行時間超過t,則拋出panic -test.cpu 1,2,4 : 程序運行在哪些CPU上面,使用二進制的1所在位代表,和nginx的nginx_worker_cpu_affinity是一個道理 -test.short : 將那些運行時間較長的測試用例運行時間縮短
Go 單元測試
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
- 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
- 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
- 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...