欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用Go進行單元測試的實現(xiàn)

 更新時間:2019年11月07日 14:52:06   作者:帥氣貓咪  
這篇文章主要介紹了使用Go進行單元測試的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

簡介

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

Go 標(biāo)準(zhǔn)庫中有一個叫做 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 目錄下創(chuàng)建一個文件 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/, 測試結(jié)果如下:

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

性能測試

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

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()
 }
}

運行結(jié)果如下(使用 --run=none 避免運行普通的測試函數(shù), 因為一般不可能有函數(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

這說明, 平均每次運行 GenShortID() 需要 2352 納秒.

性能分析

運行測試的時候, 可以指定一些參數(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)該會生成 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 選項可以統(tǒng)計測試覆蓋率.使用 go tool cover -func=cover.out 可以查看更加詳細的測試覆蓋率結(jié)果,
統(tǒng)計每個函數(shù)的測試覆蓋率.

總結(jié)

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

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

作為版本 v0.15.0

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • go?singleflight緩存雪崩源碼分析與應(yīng)用

    go?singleflight緩存雪崩源碼分析與應(yīng)用

    這篇文章主要為大家介紹了go?singleflight緩存雪崩源碼分析與應(yīng)用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-09-09
  • 構(gòu)建go鏡像實現(xiàn)過程全面講解

    構(gòu)建go鏡像實現(xiàn)過程全面講解

    這篇文章主要為大家介紹了構(gòu)建go鏡像實現(xiàn)過程全面講解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-12-12
  • Go語言斷言和類型查詢的實現(xiàn)

    Go語言斷言和類型查詢的實現(xiàn)

    Go語言變量類型包含基礎(chǔ)類型和復(fù)合類型,類型斷言一般是對基礎(chǔ)類型的處理,本文主要介紹了Go語言斷言和類型查詢的實現(xiàn),感興趣的可以了解一下
    2024-01-01
  • golang框架gin的日志處理和zap lumberjack日志使用方式

    golang框架gin的日志處理和zap lumberjack日志使用方式

    這篇文章主要介紹了golang框架gin的日志處理和zap lumberjack日志使用方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-01-01
  • Golang中重復(fù)錯誤處理的優(yōu)化方法

    Golang中重復(fù)錯誤處理的優(yōu)化方法

    這篇文章主要給大家介紹了關(guān)于Golang中重復(fù)錯誤處理優(yōu)化的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家學(xué)習(xí)或者使用Golang具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • go實現(xiàn)圖片拼接與文字書寫的方法實例

    go實現(xiàn)圖片拼接與文字書寫的方法實例

    這篇文章主要給大家介紹了關(guān)于go實現(xiàn)圖片拼接與文字書寫的相關(guān)資料,文中通過實例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2022-01-01
  • GO語言對數(shù)組切片去重的實現(xiàn)

    GO語言對數(shù)組切片去重的實現(xiàn)

    本文主要介紹了GO語言對數(shù)組切片去重的實現(xiàn),主要介紹了幾種方法,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • Golang極簡入門教程(三):并發(fā)支持

    Golang極簡入門教程(三):并發(fā)支持

    這篇文章主要介紹了Golang極簡入門教程(三):并發(fā)支持,本文講解了goroutine線程、channel 操作符等內(nèi)容,需要的朋友可以參考下
    2014-10-10
  • Golang空接口與類型斷言的實現(xiàn)

    Golang空接口與類型斷言的實現(xiàn)

    本文主要介紹了Golang空接口與類型斷言的實現(xiàn),文中根據(jù)實例編碼詳細介紹的十分詳盡,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • golang使用grpc+go-kit模擬oauth認證的操作

    golang使用grpc+go-kit模擬oauth認證的操作

    這篇文章主要介紹了golang使用grpc+go-kit模擬oauth認證的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-04-04

最新評論