[英]How to see stack trace of test code of Go program?
I use Go's native test facility ( go test
) to write a test. 我使用Go的本机测试工具(
go 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. 甚至,测试代码需要一个上下文对象
t
,因此在正常模式下运行测试代码并不简单。
What is the best practice to debug test code? 调试测试代码的最佳做法是什么?
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
您可以使用
t.Log()
记录有关测试用例的信息 - 如果测试用例失败或者运行go test -v
,go将显示该输出
You can also assert certain state within the test using panics -- if a test panics, you will see the trace in your console. 您还可以使用恐慌来确定测试中的某些状态 - 如果测试发生混乱,您将在控制台中看到跟踪。
I don't know if you'd want to check in code with this in it, but for one-off debugging, PrintStack might help. 我不知道你是否想用它来检查代码,但是对于一次性调试,PrintStack可能有所帮助。 http://golang.org/pkg/runtime/debug/#PrintStack
http://golang.org/pkg/runtime/debug/#PrintStack
You can log stack trace this way 您可以通过这种方式记录堆栈跟踪
t.Log(string(debug.Stack()))
Documentation is here https://golang.org/pkg/runtime/debug/#Stack 文档在这里https://golang.org/pkg/runtime/debug/#Stack
It is better than PrintStack
because it doesn't interfere with regular test logs. 它比
PrintStack
更好,因为它不会干扰常规测试日志。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.