Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to see stack trace of test code of Go program?

I use Go's native test facility (go test) to write a test. But when the test fails due to a bug in test code, I really can't debug it due to lack of stack trace or any other contextual informations.

And even, the test code needs one contextual object t, so it is not simple work running the test code in normal mode.

What is the best practice to debug test code?

like image 638
eonil Avatar asked Nov 03 '13 20:11

eonil


2 Answers

You can log stack trace this way

t.Log(string(debug.Stack()))

Documentation is here https://golang.org/pkg/runtime/debug/#Stack

It is better than PrintStack because it doesn't interfere with regular test logs.

like image 55
Igor Mikushkin Avatar answered Nov 15 '22 08:11

Igor Mikushkin


You can use t.Log() to log information about the test case -- go will show that output if the test case fails or if you run go test -v

You can also assert certain state within the test using panics -- if a test panics, you will see the trace in your console.

like image 25
Rob Avatar answered Nov 15 '22 08:11

Rob