How to measure Golang integration test coverage?

試著忘記壹切 提交于 2019-12-03 10:57:42

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.

As far as I know, if you want coverage you need to run go test -cover.

However it is easy enough to add a flag which you can pass in which will enable these extra tests, so you can make them part of your test suite but don't run them normally.

So add a command line flag in your whatever_test.go

var integrationTest = flag.Bool("integration-test", false, "Run the integration tests")

Then in each test do something like this

func TestSomething(t *testing.T){
    if !*integrationTest {
        t.Skip("Not running integration test")
    }
    // Do some integration testing
}

Then to run the integration tests

go run -cover -integration-test

you can run go test in a way that creates coverage html pages. like this:

go test -v -coverprofile cover.out ./...
go tool cover -html=cover.out -o cover.html
open cover.html
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!