go: testing: show diffs for incorrect output from Example tests
What version of Go are you using (go version)?
$ go version go version go1.15.1 darwin/amd64
Does this issue reproduce with the latest release?
yes
What operating system and processor architecture are you using (go env)?
go env Output
$ go env GO111MODULE="" GOARCH="amd64" GOBIN="" GOCACHE="/Users/home/Library/Caches/go-build" GOENV="/Users/home/Library/Application Support/go/env" GOEXE="" GOFLAGS="" GOHOSTARCH="amd64" GOHOSTOS="darwin" GOINSECURE="" GOMODCACHE="/Users/home/go/pkg/mod" GONOPROXY="" GONOSUMDB="" GOOS="darwin" GOPATH="/Users/home/go" GOPRIVATE="" GOPROXY="https://proxy.golang.org,direct" GOROOT="/usr/local/go" GOSUMDB="sum.golang.org" GOTMPDIR="" GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64" GCCGO="gccgo" AR="ar" CC="clang" CXX="clang++" CGO_ENABLED="1" GOMOD="/Users/home/mystuff/kama/go.mod" CGO_CFLAGS="-g -O2" CGO_CPPFLAGS="" CGO_CXXFLAGS="-g -O2" CGO_FFLAGS="-g -O2" CGO_LDFLAGS="-g -O2" PKG_CONFIG="pkg-config" GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/gc/sfs6hvtd1r392kn91n3jp17m0000gn/T/go-build416873732=/tmp/go-build -gno-record-gcc-switches -fno-common"
What did you do?
Created test example (https://golang.org/pkg/testing/#hdr-Examples, https://blog.golang.org/examples).
A reproducer of the issue is at https://play.golang.org/p/969hijP9sMx
What did you expect to see?
That example test is failing because there is a mismatch between what is been output to stdOut and what the test runner expects the output to be.
Unfortunately for test examples(unlike normal tests), I’m not able to programmatically get the test output so that I can examine(via diffing or otherwise) it and compare it to the expected result.
What did you see instead?
I do not have a way to access(programmatically) the output of the test.
About this issue
- Original URL
- State: open
- Created 6 years ago
- Comments: 15 (14 by maintainers)
There is a trap here, namely adding infinite new API to package testing as individual needs come up. We need testing to do a limited amount of things. It’s already getting hard to remember everything.
Looking at https://play.golang.org/p/969hijP9sMx, the issue I see is not that testing needs new API but instead that the example test output needs to do a better job highlighting the differences, such as by showing a diff. Maybe we should do that instead?
Based on the discussion, this seems like a likely accept.
No change in consensus, so accepted.
Come to think of it, it would be pretty straightforward to encapsulate the approach from the above comment as an external library. The API could look something like:
As another alternative, you could use
os/execto runos.Args[0]as a subprocess, to either run the generatedExampleHellotest or invoke the originalExampleHellofunction directly. (See https://play.golang.org/p/T4fDo8D5M0H, but note that it doesn’t work in the Playground due to #41339.)You could either extract the
want:output by parsing the output of the subprocess, or usego/doc.Examplesto extract it programmatically.I guess that would work somehow. But I would have to run
go testusing another program that parses that output and compares thegotandwantedparts of the output.Have you considered the
-jsonflag ofgo test? For example:The only way I was able to achieve what I want is via the following bad hack;