Go (Test)
Test failed if test coverage below 80%
func TestMain(m *testing.M) {
// call flag.Parse() here if TestMain uses flags
rc := m.Run()
// rc 0 means we've passed,
// and CoverMode will be non empty if run with -cover
if rc == 0 && testing.CoverMode() != "" {
c := testing.Coverage()
if c < 0.8 {
fmt.Println("Tests passed but coverage failed at", c)
rc = -1
}
}
os.Exit(rc)
}
Mockinng using GoMock
# install gomock
go get github.com/golang/mock/gomock
go install github.com/golang/mock/mockgen
mockgen -source=flow/kafka_admin.go -destination=flow/mock_kafka_admin.go -package=flow
Cover mode https://blog.golang.org/cover
go test -covermode=set # did each statement run?
go test -covermode=count # how many times did each statement run?
go test -covermode=atomic # like count, but counts precisely in parallel programs
Get coverage rate (source)
go test -covermode=count -coverprofile=coverage.out ./internal/... | grep coverage | cut -d ' ' -f 2 | sed 's/%//g'