Is it possible to print golang unit test results out to a file?
Asked Answered
M

4

7

I run go test in my pkg directory and I get the test results printed to the console as they run, but it would be ideal if I could get them printed to a txt file or even a html file, is it possible to do this? I know you can get coverage reports out from it and generate html files for those which is excellent, but I would have thought it possible to do the same just for the actual results of the tests i.e which tests ran, which passed and which failed. I've been searching the net but even go test help doesn't offer any suggestions for printing results out to a file.

Morganstein answered 19/3, 2015 at 17:5 Comment(3)
Does go test > test.out work?Sandman
possible duplicate of Converting Go testing output to XUnitKimmi
go test > test.out was exactly what I wanted thankyou very much @JakeBurkheadMorganstein
K
4

2018: Go 1.10+

As noted in Yoshiki Shibukawa's answer, you have cmd/test2json

go tool test2json [-p pkg] [-t] [./pkg.test -test.v=test2json]

Other approaches around go tool test2json:

  • KacperPerschke/json2test: Reverts the result of go tool test2json.
  • TaylorOno/golandreporter Ginkgo does not output results in a format which can be converted into JSON (via go tool test2json, which is used by Goland to get a list of output. This reporter will trick test2json into outputting the Ginkgo specs similar to go test output.

2015: Since I only want to see failed test, I have this script "gt" that I run instead of go test:

go test -coverprofile=coverage.out %*|grep -v -e "^\.\.*$"|grep -v "^$"|grep -v "thus far"

That way, it filters everything but the failed cases.
And you can redirect its content to a file, as mentioned: gt > test.out

It also generates code coverage, which is why I have another script "gc":

grep -v -e " 1$" coverage.out

That way, I don't even wait for a brower to open, I directly see the list of lines which are not yet covered (ie, which don't end with ' 1' in the coverage.out file)

Knowles answered 19/3, 2015 at 17:27 Comment(2)
is there a way to store the output of go test command ? I am only interested in the end result, may be just "pass" or "fail"Plethora
@Plethora You could use the exit status of the go test command: github.com/golang/go/issues/25989#issuecomment-399275051Knowles
B
4

This will append test results to the test.out file.

go test >> test.out

This will overwrite the test results for each test run.

go test > test.out
Boresome answered 22/3, 2022 at 15:0 Comment(0)
B
3

"go test -json" is available from go 1.10. It calls test2json command inside the process and generates JSON output to console.

There are several tools that consume the json and generate HTML or JUnit XML on GitHub.

Boomerang answered 28/9, 2023 at 11:32 Comment(1)
Good point. Upvoted. I have included a reference to your answer in mine.Knowles
K
1

I wanted my go test results to be printed to the console as usual, but also to be stored in JUnit format. I was able to achieve this in two steps:

  1. Use tee to append the results to a test file. I have multiple calls to go test, but in my case I wanted all the results consolidated, but if you just want a single test session captured you can drop the -a flag to tee.
tests="./..."
go test -v 2>&1 \
    $tests \
    | tee -a test-results.raw

This answers OP's question.

  1. Convert the raw test results to the JUnit format. I'm using go-junit-report and it's pleasantly straightforward:
cat test-results.raw \
    | go-junit-report -set-exit-code > test-report.xml
Kangaroo answered 8/8, 2024 at 18:31 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.