I am working on a CI(TFS2015) for our go application and I want to ensure the code quality. For example, when the code coverage of go application is low(less than 60%), the CI will fail the building process. Now I can use the "go test -coverprofile=cover.out" command to get the code coverage value successfully, but I don't know how to extract this value so it can be used in other places, such as a PowerShell script. I tried to read the cover.out file but it didn't have the coverage result.
1条回答 默认 最新
- duanquan1207 2018-03-07 20:53关注
Dumb and straightforward way I can guess right ahead:
- try
go test -cover -json
, - unmarshal the result
- and look for the "coverage:" string in "Action": "output".
The other way:
using
go tool cover -func=cover.out
you get nice and clean lines of text like that:fmt/format.go: init 100.0% fmt/format.go: clearflags 100.0% fmt/format.go: init 100.0% fmt/format.go: computePadding 84.6% fmt/format.go: writePadding 100.0% fmt/format.go: pad 100.0% ... fmt/scan.go: advance 96.2% fmt/scan.go: doScanf 96.8% total: (statements) 91.7%
it is straightforward to parse it, for example: https://play.golang.org/p/bgYW7NZKu_S.
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报 - try