简体   繁体   中英

How to extract the code coverage value from go test -coverprofile=cover.out?

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM