使用Go进行单元测试的实现
简介
日常开发中,测试是不能缺少的.
Go标准库中有一个叫做testing的测试框架,可以用于单元测试和性能测试.
它是和命令gotest集成使用的.
测试文件是以后缀_test.go命名的,通常和被测试的文件放在同一个包中.
单元测试
单元测试的格式形如:
funcTestAbs(t*testing.T){
got:=Abs(-1)
ifgot!=1{
t.Errorf("Abs(-1)=%d;want1",got)
}
}
在util目录下创建一个文件util_test.go,添加一个单元测试:
packageutil
import"testing"
//普通的测试
funcTestGenShortID(t*testing.T){
shortID,err:=GenShortID()
ifshortID==""||err!=nil{
t.Error("GenShortIDfailed")
}
}
然后,在根目录下运行gotest-v./util/,测试结果如下:
root@592402321ce7:/workspace#gotest-v./util/ ===RUNTestGenShortID ---PASS:TestGenShortID(0.00s) PASS oktzh.com/web/util0.006s
性能测试
性能测试的结果形如:
funcBenchmarkHello(b*testing.B){
fori:=0;i
在util_test.go添加性能测试:
//性能测试
funcBenchmarkGenShortID(b*testing.B){
fori:=0;i
运行结果如下(使用--run=none避免运行普通的测试函数,因为一般不可能有函数名匹配none):
root@592402321ce7:/workspace#gotest-v-bench="BenchmarkGenShortID$"--run=none./util/
goos:linux
goarch:amd64
pkg:tzh.com/web/util
BenchmarkGenShortID-25072372352ns/op
PASS
oktzh.com/web/util1.229s
这说明,平均每次运行GenShortID()需要2352纳秒.
性能分析
运行测试的时候,可以指定一些参数,生成性能文件profile.
-blockprofileblock.out
Writeagoroutineblockingprofiletothespecifiedfile
whenalltestsarecomplete.
Writestestbinaryas-cwould.
-blockprofileraten
Controlthedetailprovidedingoroutineblockingprofilesby
callingruntime.SetBlockProfileRatewithn.
See'godocruntime.SetBlockProfileRate'.
Theprofileraimstosample,onaverage,oneblockingeventevery
nnanosecondstheprogramspendsblocked.Bydefault,
if-test.blockprofileissetwithoutthisflag,allblockingevents
arerecorded,equivalentto-test.blockprofilerate=1.
-coverprofilecover.out
Writeacoverageprofiletothefileafteralltestshavepassed.
Sets-cover.
-cpuprofilecpu.out
WriteaCPUprofiletothespecifiedfilebeforeexiting.
Writestestbinaryas-cwould.
-memprofilemem.out
Writeanallocationprofiletothefileafteralltestshavepassed.
Writestestbinaryas-cwould.
-memprofileraten
Enablemoreprecise(andexpensive)memoryallocationprofilesby
settingruntime.MemProfileRate.See'godocruntime.MemProfileRate'.
Toprofileallmemoryallocations,use-test.memprofilerate=1.
-mutexprofilemutex.out
Writeamutexcontentionprofiletothespecifiedfile
whenalltestsarecomplete.
Writestestbinaryas-cwould.
-mutexprofilefractionn
Sample1innstacktracesofgoroutinesholdinga
contendedmutex.
使用下面的命令,生成CPU的profile:
gotest-v-bench="BenchmarkGenShortID$"--run=none-cpuprofilecpu.out./util/
当前目录下,应该会生成cpu.out文件和util.test文件.
使用下面的命令,观察耗时操作:
#进入交互模式
gotoolpprofcpu.out
top
安装Graphviz后可以生成可视化的分析图.
aptinstallgraphviz
gotoolpprof-http=":"cpu.out
测试覆盖率
root@592402321ce7:/workspace#gotest-v-coverprofile=cover.out./util/
===RUNTestGenShortID
---PASS:TestGenShortID(0.00s)
PASS
coverage:9.1%ofstatements
oktzh.com/web/util0.005scoverage:9.1%ofstatements
root@592402321ce7:/workspace#gotoolcover-func=cover.out
tzh.com/web/util/util.go:12:GenShortID100.0%
tzh.com/web/util/util.go:17:GetReqID0.0%
tzh.com/web/util/util.go:22:TimeToStr0.0%
tzh.com/web/util/util.go:30:GetTag0.0%
total:(statements)9.1%
使用-coverprofile=cover.out选项可以统计测试覆盖率.使用gotoolcover-func=cover.out可以查看更加详细的测试覆盖率结果,
统计每个函数的测试覆盖率.
总结
测试是开发中非常重要的一个环节,用于保证软件质量,切不可偷懒.
当前部分的代码
作为版本v0.15.0
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。