使用Go进行单元测试的实现
作者:帅气猫咪 发布时间:2024-04-25 15:11:07
标签:Go,单元测试
简介
日常开发中, 测试是不能缺少的.
Go 标准库中有一个叫做 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 目录下创建一个文件 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 可以查看更加详细的测试覆盖率结果,
统计每个函数的测试覆盖率.
总结
测试是开发中非常重要的一个环节, 用于保证软件质量, 切不可偷懒.
当前部分的代码
作为版本 v0.15.0
来源:https://juejin.im/post/5dc37eb8e51d452a066999bf
0
投稿
猜你喜欢
- python-redis-lock官方文档不错的博文可参考问题背景在使用celery执行我们的异步任务时,为了提高效率,celery可以开启
- 如果你是个学生,你应该会C,C++和Java。还会一些VB,或C#/.NET。多少你还可能开发过一些Web网页,你知道一些HTML,CSS和
- 背景介绍作为一款产品,往往希望能得到用户的反馈,从而通过对用户行为的分析进行功能、交互等方面的改进。然而直接的一对一的用户交流是低效且困难的
- 内连接(inner join)。 外连接: 全连接(full join)、左连接(left join)、右连接(right join)。 交
- 目录1. 前言2. 准备3. 实战1、获取目标应用的包名及初始化 Activity2、获取所有在线的设备3、群控打开目标应用4、封装执行步骤
- 1. 前言由于公司的一个项目是基于B/S架构与WEB服务通信,使用XML数据作为通信数据,在添加新功能时,WEB端与客户端分别由不同的部门负
- 1,使用mysqldump时报错(1064),这个是因为mysqldump版本太低与当前数据库版本不一致导致的。mysqldump: Cou
- 1、python安装可以跨平台2、有两个版本2.7和3.6,第三方库适用2.7版,两个版本不兼容windows安装:第一种方法官网安装:在官
- 文中用到了BeautifulSoup这个库, 目的是处理html文档分析的, 因为我只是提取了title的关键字,所以可以用正则表达式代替,
- 本文实例讲述了Python模块的制作方法。分享给大家供大家参考,具体如下:1 目的利用setup.py将框架安装到python环境中,作为第
- 本文实例为大家分享了微信小程序实现吸顶盒效果的具体代码,供大家参考,具体内容如下html部分 <!-- 列表 -->&
- 本文实例讲述了mysql中各种常见join连表查询。分享给大家供大家参考,具体如下:通常我们需要连接多个表查询数据,以获取想要的结果。一、连
- HTML5,被传为Flash 的杀手,是一种用于web 应用程序开发、具有变革意义的网络技术。HTML 5提供了一些新的元素和属性,其中有些
- 1.CNN概述CNN的整体思想,就是对图片进行下采样,让一个函数只学一个图的一部分,这样便得到少但是更有效的特征,最后通过全连接神经网络对结
- 在正式编写爬虫案例前,先对 scrapy 进行一下系统的学习。scrapy 安装与简单运行使用命令 pip install scrapy 进
- 这两天看了下某位大神的github,知道他对算法比较感兴趣,看了其中的一个计算数字的步数算法,感觉这个有点意思,所以就自己实现了一个。算法描
- 1.apache 在如下页面下载apache的for Linux 的源码包 http://www
- 在这个文章中,我们将学习如何在感兴趣区域周围画最小面积矩形框。1.最小面积矩形框下图显示了两个矩形框,绿色的是普通矩形框,红色的是最小面积矩
- 【问题原因】 这个应该是 jquery.datatable 控件本身的一个缺陷。该控件中的checkbox小插件的 id是写死的,所以当 有
- 输入框组默认是div.input-group已知可在input表单元素前后加入两类元素【分别是文本和按钮】如下所示:div.input-gr