Home > Backend Development > Golang > Why Does Go Coverage Exclude Functions in External Packages?

Why Does Go Coverage Exclude Functions in External Packages?

Barbara Streisand
Release: 2024-11-29 06:46:10
Original
481 people have browsed it

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
Copy after login

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 ./...
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template