拾遗:Go单元测试
概念
概要
基础测试
# -v 显示详细信息,-cover 显示测试覆盖率 go test -v -cover=true $GOPATH/src/examples/example_test.go $GOPATH/src/examples/example.go
go test -v -cover=true $GOPATH/src/examples/...
go test -v -cover=true $GOPATH/src/examples -run 'TestSucc|XxYyZz'
表格驱动测试:TableDrivenTests (https://github.com/cweill/gotests)
go get -u github.com/cweill/gotests/...
gotests [options] PATH ...
-all generate go tests for all functions and methods
-excl regexp. generate go tests for functions and methods that don't match. Takes precedence over(优先于) -only, and -all
-only regexp. generate go tests for functions and methods that match only. Takes precedence over -all
-w write output to (test) files instead of stdout
-nosubtests disable subtest generation. Only available for Go 1.7+
# 在当前目录下生成测试代码,使用 -w 选项会自动生成 example_test.go 文件,在其中的 TO DO 位置添加测试输入条目即可
gotests -all example.go > example_test.go
Mock (https://github.com/golang/mock)
以可控的模拟对象替换真实对象,以获取测试结果
go get github.com/golang/mock/gomock
go get github.com/golang/mock/mockgen
#go doc code.google.com/p/gomock/gomock
mockgen -source {SourceFile}.go > {DestFile}.go
Go monkeypatching (https://github.com/bouk/monkey)
参考资料: