Reputation: 85955
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?
Upvotes: 8
Views: 8321
Reputation: 1317
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.
Upvotes: 9
Reputation: 22116
I don't know if you'd want to check in code with this in it, but for one-off debugging, PrintStack might help. http://golang.org/pkg/runtime/debug/#PrintStack
Upvotes: 2
Reputation: 1365
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.
Upvotes: 5