好湿?好紧?好多水好爽自慰,久久久噜久噜久久综合,成人做爰A片免费看黄冈,机机对机机30分钟无遮挡

主頁 > 知識庫 > 使用Go進行單元測試的實現

使用Go進行單元測試的實現

熱門標簽:浙江高速公路地圖標注 高德地圖標注口訣 南通如皋申請開通400電話 中國地圖標注省會高清 地圖標注的汽車標 西部云谷一期地圖標注 江西轉化率高的羿智云外呼系統 學海導航地圖標注 廣州呼叫中心外呼系統

簡介

日常開發中, 測試是不能缺少的.

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

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

您可能感興趣的文章:
  • 深入理解Golang的單元測試和性能測試

標簽:阿克蘇 太原 調研邀請 貴陽 德州 西雙版納 慶陽 廣西

巨人網絡通訊聲明:本文標題《使用Go進行單元測試的實現》,本文關鍵詞  使用,進行,單元,測試,的,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《使用Go進行單元測試的實現》相關的同類信息!
  • 本頁收集關于使用Go進行單元測試的實現的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 鲁啊鲁在线视频| 国产凌凌漆经典对白| 操操操日日日| 别揉我奶胸?啊按摩| 美女任你摸视频| 戛纳电影节历年获奖名单| 国产精品无码无卡毛片不卡视频| 无限资源2018免费观看在线播放| 1000部未满岁18禁止观看免| zz236宅宅最新伦理| 亚洲va欧美| 女的被?到爽的羞羞动态图| 名器人妻被各种姿势H| 波多野结衣大战黑人k八| 极品videossex日本妇| 轮j小说| 日本三级无码中文字幕西瓜影视| 一级特黄特色的免费大片视频 | 久久久精品AV| 91精品酒店情侣在线播放 | 色哟哟丨小泬丨国产专区| 老妇女bbwbbwbbw| 日韩精品高清自在线| 高清一区二区在线观看| 波多野结衣在线资源观看| 美女又爽?又黄?网站泳装| 又黄又爽又色的性视频| 揉我奶头?啊?嗯高潮gif| 毛片无码一区二区三区A片视频| 古代的一A一片一级一片| 范冰冰xxxx性| 女仆扒开腿秘?让人桶爽的作文 | 亚洲图片小说综合| 韩国恐怖片2022| 美艳人妻佐佐木明希AV| 欧美日韩Av无码精品色情60后| 国产老头老太一级A片| fc2精品成人免费观看| 99久热只有精品视频免费观看17| 日本免费的一级绿象| 看黄a大片真人视频|