Home > Backend Development > Golang > How Can I Include External Package Calls in My Go Coverage Report?

How Can I Include External Package Calls in My Go Coverage Report?

Linda Hamilton
Release: 2024-12-01 10:27:10
Original
909 people have browsed it

How Can I Include External Package Calls in My Go Coverage Report?

Including Package Calls in Go Coverage

Problem:

In a Go project with a multi-package structure, certain functions in packages outside of the currently tested package are being ignored by the coverage report. Specifically, functions in bar.go located in the db package are not showing any coverage despite being called from foo.go in the api package.

Solution:

To resolve this, the -coverpkg flag should be added to the go test command. The -coverpkg flag specifies which packages to include in the coverage report.

Explanation:

By default, the coverage report only includes packages that are directly imported by the test package. In this case, foo_test.go (the test package for foo.go) does not directly import db, so the functions in bar.go are not being covered.

Adding the -coverpkg flag explicitly includes the specified packages in the coverage analysis. In this case, the following command would include all packages in the current directory (./...) in the coverage report:

go test -coverpkg=./... coverprofile=coverage.out ./...
Copy after login

This will generate a coverage report that includes the function call to bar.go from foo.go.

Note:

Using the -coverpkg flag may impact the test execution time, as it requires analyzing a larger number of packages. However, it is necessary to obtain accurate coverage information for cross-package function calls.

The above is the detailed content of How Can I Include External Package Calls in My Go Coverage Report?. 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