最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

簡介

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

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

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

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

單元測試

單元測試的格式形如:

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

package util

import "testing"

// 普通的測試
func TestGenShortID(t *testing.T) {
 shortID, err := GenShortID()
 if shortID == "" || err != nil {
 t.Error("GenShortID failed")
 }
}

然后, 在根目錄下運(yùn)行 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()
 }
}

運(yùn)行結(jié)果如下(使用 --run=none 避免運(yùn)行普通的測試函數(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

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

性能分析

運(yùn)行測試的時(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)該會生成 cpu.out 文件和 util.test 文件.

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

# 進(jìn)入交互模式
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 選項(xiàng)可以統(tǒng)計(jì)測試覆蓋率.使用 go tool cover -func=cover.out 可以查看更加詳細(xì)的測試覆蓋率結(jié)果,
統(tǒng)計(jì)每個(gè)函數(shù)的測試覆蓋率.

總結(jié)

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

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

作為版本 v0.15.0

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

相關(guān)文章

  • Go操作各大消息隊(duì)列教程(RabbitMQ、Kafka)

    Go操作各大消息隊(duì)列教程(RabbitMQ、Kafka)

    消息隊(duì)列是一種異步的服務(wù)間通信方式,適用于無服務(wù)器和微服務(wù)架構(gòu),本文主要介紹了Go操作各大消息隊(duì)列教程(RabbitMQ、Kafka),需要的朋友可以了解一下
    2024-02-02
  • GoLang 中的隨機(jī)數(shù)的示例代碼

    GoLang 中的隨機(jī)數(shù)的示例代碼

    本篇文章主要介紹了GoLang 中的隨機(jī)數(shù)的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-03-03
  • go?zero微服務(wù)實(shí)戰(zhàn)系服務(wù)拆分

    go?zero微服務(wù)實(shí)戰(zhàn)系服務(wù)拆分

    這篇文章主要為大家介紹了go?zero微服務(wù)實(shí)戰(zhàn)系服務(wù)拆分的示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-06-06
  • Go語言普通指針unsafe.Pointer?uintpt之間的關(guān)系及指針運(yùn)算

    Go語言普通指針unsafe.Pointer?uintpt之間的關(guān)系及指針運(yùn)算

    這篇文章主要為大家介紹了Go語言普通指針unsafe.Pointer?uintpt之間的關(guān)系及指針運(yùn)算示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-12-12
  • go?mongox簡潔高效文檔操作及bson數(shù)據(jù)構(gòu)造流暢技巧

    go?mongox簡潔高效文檔操作及bson數(shù)據(jù)構(gòu)造流暢技巧

    這篇文章主要為大家介紹了go?mongox簡潔高效文檔操作及bson數(shù)據(jù)構(gòu)造流暢技巧示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-11-11
  • Go語言實(shí)現(xiàn)分布式鎖

    Go語言實(shí)現(xiàn)分布式鎖

    分布式鎖是控制分布式系統(tǒng)之間同步訪問共享資源的一種方式。如果不同的系統(tǒng)或是同一個(gè)系統(tǒng)的不同主機(jī)之間共享了一個(gè)或一組資源,那么訪問這些資源時(shí),需要通過一些互斥手段來防止彼此之間的干擾以保證一致性,在這種情況下,就需要使用分布式鎖了
    2023-01-01
  • 詳解Go語言中iota的應(yīng)用

    詳解Go語言中iota的應(yīng)用

    在本文中,小編將帶著大家深入探討?iota?的神奇力量,包括?iota?的介紹和應(yīng)用場景以及使用技巧和注意事項(xiàng),準(zhǔn)備好了嗎,準(zhǔn)備一杯你最喜歡的飲料或茶,隨著本文一探究竟吧
    2023-07-07
  • Go語言的文件操作代碼匯總

    Go語言的文件操作代碼匯總

    本文給大家匯總介紹了go語言中的文件操作的代碼,包括文件的讀寫,文件的新建打開和刪除等,希望對大家學(xué)習(xí)go語言能夠有所幫助
    2018-10-10
  • Go讀取配置文件的方法總結(jié)

    Go讀取配置文件的方法總結(jié)

    我們常見的配置文件的格式一般有:XML、JSON、INI、YAML、env和.properties,本文小編為大家整理了Go語言讀取這些格式的配置文件的方法,希望對大家有所幫助
    2023-10-10
  • Golang使用Gin框架實(shí)現(xiàn)路由分類處理請求流程詳解

    Golang使用Gin框架實(shí)現(xiàn)路由分類處理請求流程詳解

    Gin是一個(gè)golang的微框架,封裝比較優(yōu)雅,具有快速靈活,容錯(cuò)方便等特點(diǎn),這篇文章主要介紹了Golang使用Gin框架實(shí)現(xiàn)路由分類處理請求,感興趣的同學(xué)可以參考下文
    2023-05-05

最新評論

扎兰屯市| 锡林浩特市| 翁牛特旗| 比如县| 通渭县| 忻城县| 扎赉特旗| 乌海市| 巩留县| 岱山县| 七台河市| 宣化县| 兴山县| 潼南县| 赤城县| 喀喇沁旗| 通榆县| 彭水| 英山县| 怀安县| 合山市| 松江区| 林甸县| 绥江县| 昌邑市| 资溪县| 通渭县| 瑞金市| 达孜县| 澄城县| 湖南省| 新蔡县| 延庆县| 津南区| 班玛县| 阿图什市| 靖江市| 大埔区| 岳普湖县| 大同县| 冀州市|