濮阳杆衣贸易有限公司

主頁(yè) > 知識(shí)庫(kù) > 使用Go進(jìn)行單元測(cè)試的實(shí)現(xiàn)

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

熱門(mén)標(biāo)簽:浙江高速公路地圖標(biāo)注 高德地圖標(biāo)注口訣 南通如皋申請(qǐng)開(kāi)通400電話(huà) 中國(guó)地圖標(biāo)注省會(huì)高清 地圖標(biāo)注的汽車(chē)標(biāo) 西部云谷一期地圖標(biāo)注 江西轉(zhuǎn)化率高的羿智云外呼系統(tǒng) 學(xué)海導(dǎo)航地圖標(biāo)注 廣州呼叫中心外呼系統(tǒng)

簡(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è)試的格式形如:

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è)試:

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é)果如下:

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

性能測(cè)試

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

func BenchmarkHello(b *testing.B) {
 for i := 0; i  b.N; i++ {
  fmt.Sprintf("hello")
 }
}

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

// 性能測(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):

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.

-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/

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

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

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

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

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

測(cè)試覆蓋率

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í)有所幫助,也希望大家多多支持腳本之家。

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

標(biāo)簽:阿克蘇 太原 調(diào)研邀請(qǐng) 貴陽(yáng) 德州 西雙版納 慶陽(yáng) 廣西

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《使用Go進(jìn)行單元測(cè)試的實(shí)現(xiàn)》,本文關(guān)鍵詞  使用,進(jìn)行,單元,測(cè)試,的,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《使用Go進(jìn)行單元測(cè)試的實(shí)現(xiàn)》相關(guān)的同類(lèi)信息!
  • 本頁(yè)收集關(guān)于使用Go進(jìn)行單元測(cè)試的實(shí)現(xiàn)的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    琼海市| 汝州市| 安顺市| 门头沟区| 瓮安县| 安丘市| 建湖县| 巴彦淖尔市| 本溪市| 正安县| 连城县| 鲁山县| 平乐县| 西藏| 普兰店市| 自贡市| 高密市| 英德市| 那坡县| 堆龙德庆县| 鄯善县| 新昌县| 无棣县| 武山县| 加查县| 九龙县| 海阳市| 阿鲁科尔沁旗| 松原市| 云南省| 长春市| 哈巴河县| 长顺县| 栖霞市| 奎屯市| 公安县| 河曲县| 沂水县| 航空| 兰溪市| 宾川县|