激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

腳本之家,腳本語(yǔ)言編程技術(shù)及教程分享平臺(tái)!
分類導(dǎo)航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務(wù)器之家 - 腳本之家 - Golang - 使用Go進(jìn)行單元測(cè)試的實(shí)現(xiàn)

使用Go進(jìn)行單元測(cè)試的實(shí)現(xiàn)

2020-05-29 13:37帥氣貓咪 Golang

這篇文章主要介紹了使用Go進(jìn)行單元測(cè)試的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

簡(jiǎn)介

日常開(kāi)發(fā)中, 測(cè)試是不能缺少的.

Go 標(biāo)準(zhǔn)庫(kù)中有一個(gè)叫做 testing 的測(cè)試框架, 可以用于單元測(cè)試和性能測(cè)試.

它是和命令 go test 集成使用的.

測(cè)試文件是以后綴 _test.go 命名的, 通常和被測(cè)試的文件放在同一個(gè)包中.

單元測(cè)試

單元測(cè)試的格式形如:

?
1
2
3
4
5
6
func TestAbs(t *testing.T) {
 got := Abs(-1)
 if got != 1 {
  t.Errorf("Abs(-1) = %d; want 1", got)
 }
}

在 util 目錄下創(chuàng)建一個(gè)文件 util_test.go, 添加一個(gè)單元測(cè)試:

?
1
2
3
4
5
6
7
8
9
10
11
package util
 
import "testing"
 
// 普通的測(cè)試
func TestGenShortID(t *testing.T) {
 shortID, err := GenShortID()
 if shortID == "" || err != nil {
 t.Error("GenShortID failed")
 }
}

然后, 在根目錄下運(yùn)行 go test -v ./util/, 測(cè)試結(jié)果如下:

?
1
2
3
4
5
root@592402321ce7:/workspace# go test -v ./util/
=== RUN  TestGenShortID
--- PASS: TestGenShortID (0.00s)
PASS
ok   tzh.com/web/util    0.006s

性能測(cè)試

性能測(cè)試的結(jié)果形如:

?
1
2
3
4
5
func BenchmarkHello(b *testing.B) {
 for i := 0; i < b.N; i++ {
  fmt.Sprintf("hello")
 }
}

在 util_test.go 添加性能測(cè)試:

?
1
2
3
4
5
6
// 性能測(cè)試
func BenchmarkGenShortID(b *testing.B) {
 for i := 0; i < b.N; i++ {
 GenShortID()
 }
}

運(yùn)行結(jié)果如下(使用 --run=none 避免運(yùn)行普通的測(cè)試函數(shù), 因?yàn)橐话悴豢赡苡泻瘮?shù)名匹配 none):

?
1
2
3
4
5
6
7
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

這說(shuō)明, 平均每次運(yùn)行 GenShortID() 需要 2352 納秒.

性能分析

運(yùn)行測(cè)試的時(shí)候, 可以指定一些參數(shù), 生成性能文件 profile.

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
-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:

?
1
go test -v -bench="BenchmarkGenShortID$" --run=none -cpuprofile cpu.out ./util/

當(dāng)前目錄下, 應(yīng)該會(huì)生成 cpu.out 文件和 util.test 文件.

使用下面的命令, 觀察耗時(shí)操作:

?
1
2
3
# 進(jìn)入交互模式
go tool pprof cpu.out
top

安裝 Graphviz 后可以生成可視化的分析圖.

?
1
2
apt install graphviz
go tool pprof -http=":" cpu.out

測(cè)試覆蓋率

?
1
2
3
4
5
6
7
8
9
10
11
12
13
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 選項(xiàng)可以統(tǒng)計(jì)測(cè)試覆蓋率.使用 go tool cover -func=cover.out 可以查看更加詳細(xì)的測(cè)試覆蓋率結(jié)果,
統(tǒng)計(jì)每個(gè)函數(shù)的測(cè)試覆蓋率.

總結(jié)

測(cè)試是開(kāi)發(fā)中非常重要的一個(gè)環(huán)節(jié), 用于保證軟件質(zhì)量, 切不可偷懶.

當(dāng)前部分的代碼

作為版本 v0.15.0

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

原文鏈接:https://juejin.im/post/5dc37eb8e51d452a066999bf

延伸 · 閱讀

精彩推薦
  • Golanggolang 通過(guò)ssh代理連接mysql的操作

    golang 通過(guò)ssh代理連接mysql的操作

    這篇文章主要介紹了golang 通過(guò)ssh代理連接mysql的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧...

    a165861639710342021-03-08
  • Golanggolang如何使用struct的tag屬性的詳細(xì)介紹

    golang如何使用struct的tag屬性的詳細(xì)介紹

    這篇文章主要介紹了golang如何使用struct的tag屬性的詳細(xì)介紹,從例子說(shuō)起,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看...

    Go語(yǔ)言中文網(wǎng)11352020-05-21
  • Golanggo語(yǔ)言制作端口掃描器

    go語(yǔ)言制作端口掃描器

    本文給大家分享的是使用go語(yǔ)言編寫的TCP端口掃描器,可以選擇IP范圍,掃描的端口,以及多線程,有需要的小伙伴可以參考下。 ...

    腳本之家3642020-04-25
  • Golanggolang json.Marshal 特殊html字符被轉(zhuǎn)義的解決方法

    golang json.Marshal 特殊html字符被轉(zhuǎn)義的解決方法

    今天小編就為大家分享一篇golang json.Marshal 特殊html字符被轉(zhuǎn)義的解決方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧 ...

    李浩的life12792020-05-27
  • Golanggo日志系統(tǒng)logrus顯示文件和行號(hào)的操作

    go日志系統(tǒng)logrus顯示文件和行號(hào)的操作

    這篇文章主要介紹了go日志系統(tǒng)logrus顯示文件和行號(hào)的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧...

    SmallQinYan12302021-02-02
  • Golanggolang的httpserver優(yōu)雅重啟方法詳解

    golang的httpserver優(yōu)雅重啟方法詳解

    這篇文章主要給大家介紹了關(guān)于golang的httpserver優(yōu)雅重啟的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,...

    helight2992020-05-14
  • GolangGolang通脈之?dāng)?shù)據(jù)類型詳情

    Golang通脈之?dāng)?shù)據(jù)類型詳情

    這篇文章主要介紹了Golang通脈之?dāng)?shù)據(jù)類型,在編程語(yǔ)言中標(biāo)識(shí)符就是定義的具有某種意義的詞,比如變量名、常量名、函數(shù)名等等,Go語(yǔ)言中標(biāo)識(shí)符允許由...

    4272021-11-24
  • GolangGolang中Bit數(shù)組的實(shí)現(xiàn)方式

    Golang中Bit數(shù)組的實(shí)現(xiàn)方式

    這篇文章主要介紹了Golang中Bit數(shù)組的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧...

    天易獨(dú)尊11682021-06-09
主站蜘蛛池模板: 久久久久久久久久久久久久久久久久 | 日韩在线黄色片 | 水多视频在线观看 | 亚洲尻逼视频 | 孕妇体内谢精满日本电影 | 亚洲最新无码中文字幕久久 | 91精品国产刺激国语对白 | 欧美a视频在线观看 | 黄色网页在线观看 | 亚洲男人天堂 | 欧美激情第一区 | 日本精品二区 | 久久精品a一级国产免视看成人 | 国产在线观看免费视频软件 | 99久久超碰中文字幕伊人 | 欧美黄色一级生活片 | 黄色网址免费入口 | 日本黄色一级视频 | 日本免费aaa观看 | 欧美又黄又嫩大片a级 | 爱逼爱操综合网 | 最新福利在线 | 在线 日本 制服 中文 欧美 | 国毛片| 国产精品美女久久久免费 | 久久无| 欧美成人精品一区二区 | free国产hd老熟bbw | 日韩精品一区二 | 欧美一级高潮 | 欧美一级全黄 | 久久精品日产第一区二区三区 | 一级做a爱性色毛片免费1 | aaaaaaa毛片 | 成人免费一区二区三区视频网站 | 亚洲国产精品久久久久制服红楼梦 | 成人免费在线视频播放 | 国产无遮挡一区二区三区毛片日本 | 香蕉国产片 | 亚洲第一精品在线 | 草久视频在线观看 |