Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

Getting new coverage output format from go test -cover

277 views
Skip to first unread message

Paul Chesnais (papacharlie)

unread,
Dec 3, 2024, 1:26:04 PM12/3/24
to golang-nuts
Hello,

I'd like to be able to merge the data from the output of `go test -cover` with the output of a run using `go build -cover` but it seems that there's no way to translate the "legacy" format from go test into the new binary format. This means it's not possible to use the built-in tooling like `go tool covdata merge`. There is of course the possibility of translating the binary format back into the legacy format using textfmt but then there's still no good way to merge the coverage data without using some custom tooling like https://github.com/wadey/gocovmerge.

As far as I can tell there's also no way to get clever and get `go test -c` to build with the binary with the `-cover` flag because that build flag gets eaten up by `go test`. It seems there's no way to get the new coverage format from go test, unless I'm missing something?

Just looking for any insights on this, thanks in advance!

Paul

Paul Chesnais (papacharlie)

unread,
Jan 11, 2025, 1:07:21 PMJan 11
to golang-nuts
Is there another forum I can ask about this? Should this be a feature request against the go team directly? It seems strange that the formats haven't been converged, or that there's no way to get the new binary format from `go test`

Cheers,

Paul

Ian Lance Taylor

unread,
Jan 11, 2025, 9:26:40 PMJan 11
to Paul Chesnais (papacharlie), golang-nuts
On Sat, Jan 11, 2025 at 10:07 AM Paul Chesnais (papacharlie)
<paul.c...@gmail.com> wrote:
>
> Is there another forum I can ask about this? Should this be a feature request against the go team directly? It seems strange that the formats haven't been converged, or that there's no way to get the new binary format from `go test`

I don't really know the answer, but the blog post
https://go.dev/blog/integration-test-coverage#working-with-coverage-data-files
discusses converting between different coverage profile data formats.
If that doesn't help, can you explain in more detail, perhaps with a
series of commands that doesn't work as you hope?

Ian


> On Tuesday, December 3, 2024 at 1:26:04 PM UTC-5 Paul Chesnais (papacharlie) wrote:
>>
>> Hello,
>>
>> I'd like to be able to merge the data from the output of `go test -cover` with the output of a run using `go build -cover` but it seems that there's no way to translate the "legacy" format from go test into the new binary format. This means it's not possible to use the built-in tooling like `go tool covdata merge`. There is of course the possibility of translating the binary format back into the legacy format using textfmt but then there's still no good way to merge the coverage data without using some custom tooling like https://github.com/wadey/gocovmerge.
>>
>> As far as I can tell there's also no way to get clever and get `go test -c` to build with the binary with the `-cover` flag because that build flag gets eaten up by `go test`. It seems there's no way to get the new coverage format from go test, unless I'm missing something?
>>
>> Just looking for any insights on this, thanks in advance!
>>
>> Paul
>
> --
> 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 visit https://groups.google.com/d/msgid/golang-nuts/d7b799e4-49a6-417d-8b13-65b5b244c11cn%40googlegroups.com.

Paul Chesnais (papacharlie)

unread,
Jan 12, 2025, 8:13:15 PM (13 days ago) Jan 12
to golang-nuts
The blog post doesn't provide a way to translate the output of `go test -coverprofile ...` (which is the so-called legacy text format) into the new binary format, which `go tool covdata merge` expects. It only provides it in the reverse, i.e. the new binary format to the legacy format. So it means if I'm running my tests with multiple invocations of `go test` (e.g. because they need to be run in different envs/different tags) there's no easy way to get merged coverage results. Is there any way to get `go test` to produce the new format, or to translate the text format into the new format?

Byungjun You

unread,
Jan 14, 2025, 8:27:07 AM (12 days ago) Jan 14
to golang-nuts
Have you try go test with -args -test.gocoverdir parameters?

> go test -cover ./... -args -test.gocoverdir="{a directory where coverage reports should be generated}"

I found this blog post is very helpful when merging unit and integration test coverage reports.
https://dustinspecker.com/posts/go-combined-unit-integration-code-coverage/

2025년 1월 13일 월요일 오전 10시 13분 15초 UTC+9에 Paul Chesnais (papacharlie)님이 작성:

Paul Chesnais (papacharlie)

unread,
Jan 21, 2025, 8:52:54 PM (4 days ago) Jan 21
to golang-nuts
Oh that's perfect! Pretty much what I was looking for. Let me test it and get back to you.

I wonder where the blog author found that flag? It's not exposed in `go test`

Byungjun You

unread,
Jan 23, 2025, 11:14:05 PM (2 days ago) Jan 23
to golang-nuts
I'm so glad to hear it helped you!

> I wonder where the blog author found that flag? It's not exposed in `go test`

I guess "-test.gocoverdir" parameter is not exposed to go test users well. There is an another comment that it was very hard to find documents about "-test.gocoverdir" parameter. :(
> Btw, I can't find documentation on -test.gocoverdir in Google, except the insides of code. Also `go help testflag | grep gocoverdir` doesn't return anything -- in go1.21.0.
2025년 1월 22일 수요일 오전 10시 52분 54초 UTC+9에 Paul Chesnais (papacharlie)님이 작성:
Reply all
Reply to author
Forward
0 new messages