Is it possible to produce coverage profiles for *_test.go files?

109 views
Skip to first unread message

Piers Powlesland

unread,
May 19, 2021, 6:23:38 AM5/19/21
to golang-nuts
I'm working on a project with some quite complex test code and I'd like to be able to understand what parts of it are executing for certain tests. I generated coverage using the go test -coverprofile=coverage.out xxxx command but the coverage file only seems to contain information for non test files. Is it possible to extend the coverage to cover *_test.go files as well?

Thanks,

Piers

Rob Pike

unread,
May 19, 2021, 6:07:49 PM5/19/21
to Piers Powlesland, golang-nuts
You could run go tool cover yourself, but getting the details right can be difficult. Instead, I suggest moving the test code into the main package, but protected by a build tag and called from the real test.

foo_test.go:

func TestFoo(t *testing.T) { mainTestFoo(t) }

foo.go:

func mainTestFoo(t *testing.T) { t.Helper(); ... }

The disadvantage is that you'd need to pass the build tag when you run the test, but that is easy to do in a script.

-rob


--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/8b8ebbac-10fe-4fde-9f54-30cf53e2321dn%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages