Home >Backend Development >Golang >Why Does Go Coverage Exclude Functions in External Packages?
Go Coverage Excludes Functions in External Packages
When running code coverage on a Go project containing multiple packages, you may encounter a situation where functions in packages other than the one under test are not included in the coverage report. This can occur when a function in one package calls a function in another package.
Problem:
As described in the question provided, a project with a package structure similar to the following exhibits this issue:
app/ api/ foo.go test_foo.go src/ db/ bar.go
foo.go calls a function in bar.go. However, the coverage report for bar.go shows zero lines covered.
Solution:
To resolve this issue, you need to include the coverpkg flag when running the coverage test. This flag specifies that coverage should be collected for all packages, regardless of their location. The modified command to run the coverage test is:
go test -coverpkg=./... coverprofile=coverage.out ./...
By adding the coverpkg flag, the coverage report will now include the function calls from foo.go to bar.go.
The above is the detailed content of Why Does Go Coverage Exclude Functions in External Packages?. For more information, please follow other related articles on the PHP Chinese website!