参考

单测覆盖率

命令

#执行所有单测
go test -v -cover ./...
 
#执行某个文件中的某个某些单测
go test -v -cover -run=AddProject project_test.go
 
#导出执行结果
go test -v -run=AddProject -coverprofile=c.out
 
#执行结果展示:
go tool cover -html=c.out

go tool cover使用方法

$ go tool cover -help
Usage of 'go tool cover':
Given a coverage profile produced by 'go test':
        go test -coverprofile=c.out
 
Open a web browser displaying annotated source code:
        go tool cover -html=c.out
 
Write out an HTML file instead of launching a web browser:
        go tool cover -html=c.out -o coverage.html
 
Display coverage percentages to stdout for each function:
        go tool cover -func=c.out
 
Finally, to generate modified source code with coverage annotations
(what go test -cover does):
        go tool cover -mode=set -var=CoverageVariableName program.go
 
Flags:
  -V    print version and exit
  -func string
        output coverage profile information for each function
  -html string
        generate HTML representation of coverage profile
  -mode string
        coverage mode: set, count, atomic
  -o string
        file for output; default: stdout
  -var string
        name of coverage variable to generate (default "GoCover")
 
  Only one of -html, -func, or -mode may be set.