go-junit-report icon indicating copy to clipboard operation
go-junit-report copied to clipboard

Parser gojson incompatibility with go test's -coverprofile flag

Open forsaken628 opened this issue 3 years ago • 3 comments

When handle yy.txt

Generated report contain incorrect content like this

<testcase name="TestSrv_ServiceMap" classname="example.com/foo/pkg/labels" time="0.000">
	<error message="No test result found"></error>
</testcase>

command: go test -json -race -coverpkg=./... -covermode=atomic -coverprofile=out/cover.out.tmp ./...

go version: go 1.18

forsaken628 avatar Jun 11 '22 06:06 forsaken628

Thanks. It looks like the testcase gets attributed to the wrong package, possibly because of the order in which it appears in the output.

I don't think I've seen this before, it would be helpful if I could reproduce this output somehow. Can you share a bit more about the environment and the tests that you ran, e.g. what version of Go did you use and did you run any tests in parallel? Did you use any additional commandline options when running go test?

jstemmer avatar Jun 11 '22 13:06 jstemmer

I can confirm that I have a similar issue regardless of whether I'm using a test coverage reporting or not. After disabling the test report in JSON, the tests run stably.

medzin avatar Jul 15 '22 16:07 medzin

I managed to reproduce by testing multiple packages at a time and enabling the race detector (-race). For non-JSON output, test events are always grouped by package. When enabling JSON output this appears to not always be the case, test events from different packages may be mixed occasionally which triggers the bug you observed. Fortunately, each event in the JSON output contains the package name so this is fixable but it will require some refactoring.

Until this has been fixed you can work around this problem by disabling the race detector, or not use the JSON test output.

jstemmer avatar Jul 20 '22 13:07 jstemmer

I have been hit by the same bug now. Yes this is fixable by looking at the JSON tag.

I can have a look at this and make a PR

karelbilek avatar Feb 15 '23 19:02 karelbilek

Ah, seems like we were using stable 2.0.0 version, which doesn't have this fixed, that's the reason. Nevermind!

karelbilek avatar Feb 15 '23 19:02 karelbilek

FYI If you are using a version without this fix you can use the next trick: go test -json [...] | jq -c '{k: .Package, v: .}' | sort | jq -c '.v' | go-junit-report [...] where [...] are yours flags It sorts go test output before passing it to the go-junit-report. jq https://stedolan.github.io/ is a cli-tool for json.

altego371 avatar Mar 29 '23 05:03 altego371