dtbrd80422 2018-03-07 20:31
浏览 802
已采纳

如何从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.

  • 写回答

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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型