Testing Across Packages for Code Coverage
When running integration tests in separate packages, achieving accurate code coverage can be challenging. By default, tests analyze only the package being tested.
In your example, the integration tests reside in the "itest" package, while the code under test resides in the "hello" package. Running the tests with go test -v -coverpkg ./... ./itest results in 0% coverage because the test coverage is confined to the "itest" package.
To obtain comprehensive code coverage, the -coverpkg flag must include the package containing the code under test. The correct command is:
go test -v -coverpkg ./... ./...
With this modification, the tests will analyze both the "hello" and "itest" packages, providing the expected coverage results.
The above is the detailed content of How Can I Achieve Accurate Code Coverage When Running Integration Tests Across Separate Go Packages?. For more information, please follow other related articles on the PHP Chinese website!