go架构根据testing和cover库完成测试覆盖率,分析工具有coverfmt和gocov。cover库完成:1.导进依靠;2.应用defer cover.coverstrings()装饰检测;3.形成普及率汇报;分析工具用法:1. coverfmt形成人们可读汇报;2. gocov形成html汇报。
Go 框架中测试覆盖率的完成与分析
介绍
测试覆盖率是检验测试套件实效性的重要因素。它显示了在运行时访问的编码行的百分比。完成与分析测试覆盖率针对保证测试套件质量以及稳定性尤为重要。
完成
Go 含有几个库能够帮助实现测试覆盖率,包含:
testing: Go 标准库中带来了基本的功效。 cover: 一个更专业的库,提供了更多选项。以下是怎么使用 cover 库完成测试覆盖率:
import(
"fmt"
"testing"
"github.com/matm/go-test-coverage/cover"
)
funcExampleFunc(){
fmt.Println("Hello")
}
funcTestExampleFunc(ttesting.T){
defercover.CoverStrings()
ExampleFunc()
}
剖析
形成普及率报告后,重要的是对它进行剖析以鉴别未覆盖区域。以下是一些分析工具:
coverfmt: 将普及率结论格式化为人们可读的汇报。gocov`: 形成 HTML 汇报,其中包括普及率结论的详细信息。以下是怎么使用 coverfmt 剖析普及率汇报:
gotool cover -func=ExampleFunc.coverprofile-o=ExampleFunc.cover.html
实战案例
考虑下列程序:
packagemain
import"fmt"
funcmain(){
fmt.Println("Hello")
}
应用 cover 库完成测试覆盖率:
import(
"fmt"
"testing"
"github.com/matm/go-test-coverage/cover"
)
funcExampleFunc(){
fmt.Println("Hello")
}
funcTestExampleFunc(ttesting.T){
defercover.CoverStrings()
ExampleFunc()
}
形成普及率汇报:
gotool cover -func=ExampleFunc.coverprofile-o=ExampleFunc.cover.html
剖析普及率汇报:
coverfmtExampleFunc.coverprofile
这将显示详尽的普及率汇报,其中突显了未遮盖代码行。
以上就是Go 框架中测试覆盖率的完成与分析的详细内容,大量请关注其他类似文章!