温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

利用golang怎么实现单元测试

发布时间:2021-03-23 15:45:03 来源:亿速云 阅读:159 作者:Leah 栏目:编程语言

今天就跟大家聊聊有关利用golang怎么实现单元测试,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

单元测试

单元测试的格式形如:

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

在 util 目录下创建一个文件 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/, 测试结果如下:

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

性能测试

性能测试的结果形如:

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

运行结果如下(使用 --run=none 避免运行普通的测试函数, 因为一般不可能有函数名匹配 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 纳秒.

性能分析

运行测试的时候, 可以指定一些参数, 生成性能文件 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/

当前目录下, 应该会生成 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 选项可以统计测试覆盖率.使用 go tool cover -func=cover.out 可以查看更加详细的测试覆盖率结果,
统计每个函数的测试覆盖率.

看完上述内容,你们对利用golang怎么实现单元测试有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI