Home >Backend Development >Golang >How Can I Get Comprehensive Code Coverage Across Multiple Go Packages?
How to Collect Comprehensive Code Coverage for Multiple Go Packages
When testing large projects in Go, it can be challenging to obtain a consolidated overview of code coverage across multiple packages. While the '-cover' flag provides coverage information for individual packages, a holistic view of project-wide coverage helps assess overall code quality.
Originally, obtaining a full coverage report required a custom solution such as the bash script provided by the gosweep project. However, with the introduction of Go 1.10, a more streamlined approach became available.
Go 1.10 introduced significant enhancements to test coverage, including the ability to enable coverage for dependencies specified in the '-coverpkg' flag. By utilizing this flag, it is now possible to execute the following command to capture comprehensive code coverage:
go test -v -coverpkg=./... -coverprofile=profile.cov ./...
This command will generate a coverage profile named 'profile.cov,' which can be further analyzed using:
go tool cover -func profile.cov
This improved coverage functionality eliminates the need for complex scripts and provides a simplified method to assess overall code coverage in Go projects.
The above is the detailed content of How Can I Get Comprehensive Code Coverage Across Multiple Go Packages?. For more information, please follow other related articles on the PHP Chinese website!