How to measure Golang integration test coverage?

后端 未结 3 1492
暗喜
暗喜 2021-02-12 19:35

I am trying to use go test -cover to measure the test coverage of a service I am building. It is a REST API and I am testing it by spinning it up, making test HTTP

3条回答
  •  悲&欢浪女
    2021-02-12 19:56

    I was pointed at the -coverpkg directive, which does what I need - measures the test coverage in a particular package, even if tests that use this package and not part of it. For example:

    $ go test -cover -coverpkg mypackage ./src/api/...
    ok      /api    0.190s  coverage: 50.8% of statements in mypackage
    ok      /api/mypackage   0.022s  coverage: 0.7% of statements in mypackage
    

    compared to

    $ go test -cover ./src/api/...
    ok      /api    0.191s  coverage: 71.0% of statements
    ok      /api/mypackage   0.023s  coverage: 0.7% of statements
    

    In the example above, I have tests in main_test.go which is in package main that is using package mypackage. I am mostly interested in the coverage of package mypackage since it contains 99% of the business logic in the project.

    I am quite new to Go, so it is quite possible that this is not the best way to measure test coverage via integration tests.

提交回复
热议问题