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?
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With