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.
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 tooltest2json
.TaylorOno/golandreporter
Ginkgo does not output results in a format which can be converted into JSON (viago tool test2json
, which is used by Goland to get a list of output. This reporter will tricktest2json
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)
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
"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.
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:
- Use
tee
to append the results to a test file. I have multiple calls togo 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 totee
.
tests="./..."
go test -v 2>&1 \
$tests \
| tee -a test-results.raw
This answers OP's question.
- 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
© 2022 - 2025 — McMap. All rights reserved.
go test > test.out
work? – Sandman