Go test coverage for a file (not package)

Hello, how to get go test coverage number for a file (not package)? e.g. I have a package xyz and two .go files in it
abc.go
def.go.
I also have two test files
abc_test.go
def_test.go.
How do I get coverage numbers for abc.go and def.go separately?

go test -cover will give me for the whole package?

Once you generate the coverage file using:
go test -coverprofile=cover.out
then you can get the coverage for the individual functions in the package with this command:
go tool -func=cover.out.
If you are using some IDE like intellij, you can run the tests with coverage for the entire project then you’ll be able to get the coverage for the individual files.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.