国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 網(wǎng)站 > 幫助中心 > 正文

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

2024-07-09 22:41:14
字體:
供稿:網(wǎng)友

簡(jiǎn)介

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

Go 標(biāo)準(zhǔn)庫中有一個(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 utilimport "testing"http:// 普通的測(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)PASSok   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: linuxgoarch: amd64pkg: tzh.com/web/utilBenchmarkGenShortID-2       507237       2352 ns/opPASSok   tzh.com/web/util    1.229s

這說明, 平均每次運(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.
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 汶上县| 乌苏市| 新密市| 锡林浩特市| 丹凤县| 西乌| 通辽市| 民乐县| 巴青县| 台湾省| 廊坊市| 黑水县| 台北市| 馆陶县| 博客| 泽库县| 德昌县| 绥德县| 都江堰市| 扶绥县| 綦江县| 怀宁县| 中卫市| 美姑县| 德格县| 千阳县| 高邮市| 辽中县| 时尚| 安远县| 吴旗县| 昌平区| 东海县| 利川市| 名山县| 浠水县| 和硕县| 涟水县| 都昌县| 吴桥县| 宁夏|