How to measure code coverage in Golang?

前端 未结 11 2053
花落未央
花落未央 2020-12-22 16:12

Has anyone succeeded in generating code coverage for Go unit tests? I can\'t find a tool for that on the web.

11条回答
  •  既然无缘
    2020-12-22 17:04

    In addition to the good answers above, I find these three lines to be the simplest way to get it (which includes all packages):

    go test -v -coverprofile cover.out ./YOUR_CODE_FOLDER/...
    go tool cover -html=cover.out -o cover.html
    open cover.html
    

    Note that in the HTML file you will find a dropdown button that will direct you to all files.

提交回复
热议问题