Is there a way to get "go test" to display the stack trace when the function under test panics?
I've written a function that (for purposes of this demo) panics if passed true:
The output on go playground looks like this:
=== RUN TestMyFunc
=== RUN TestMyFunc/good
=== RUN TestMyFunc/panic
--- FAIL: TestMyFunc (0.00s)
--- PASS: TestMyFunc/good (0.00s)
--- FAIL: TestMyFunc/panic (0.00s)
panic: assignment to entry in nil map [recovered, repanicked]
goroutine 17 [running]:
testing.tRunner.func1.2({0x55a800, 0x6cac00})
/usr/local/go-faketime/src/testing/testing.go:1974 +0x232
testing.tRunner.func1()
/usr/local/go-faketime/src/testing/testing.go:1977 +0x349
panic({0x55a800?, 0x6cac00?})
/usr/local/go-faketime/src/runtime/panic.go:860 +0x13a
play.MyFunc(...)
/tmp/sandbox252008880/prog_test.go:14
play.TestMyFunc.func1(0x3e938180e008?)
/tmp/sandbox252008880/prog_test.go:33 +0x33
testing.tRunner(0x3e938180e008, 0x3e9381800000)
/usr/local/go-faketime/src/testing/testing.go:2036 +0xea
created by testing.(*T).Run in goroutine 7
/usr/local/go-faketime/src/testing/testing.go:2101 +0x4c5
Program exited.
However when I run this on my personal machine (go1.26.4, Macos 26.5.1) the system goes into an infinite loop until I press CTRL-C. At that point it exits but never shows the stack trace:
$ go test -v
=== RUN TestMyFunc
=== RUN TestMyFunc/good
=== RUN TestMyFunc/panic
--- FAIL: TestMyFunc (0.00s)
--- PASS: TestMyFunc/good (0.00s)
--- FAIL: TestMyFunc/panic (0.00s)
^Csignal: interrupt
FAIL ptest 4.924s
As you can imagine, debugging an unknown panic is difficult without the stack trace indicating the problematic line. Delve has the same behavior.
Is there a way to have "go test" display the stack trace like Go playground does?
Also... the fact that a panic sends Go testing into an infinite loop sounds like a bug. Is this just my environment, or can others reproduce this?
Thanks in advance!