How to measure code coverage in Golang?

前端 未结 11 2063
花落未央
花落未央 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:05

    A quick and easy way is to use the coverage tool that comes with built-in go :

    $ go test -coverprofile cp.out // Emits the coverage in one liner percentage wise

    After you execute the above command, if you wish to visually see the code coverage (like covered statements and missed etc)

    $ go tool cover -html=cp.out

    Note : You need to execute the above commands in the folder where you wish to see coverage

提交回复
热议问题