dongpou1935
2013-11-03 20:05
浏览 340
已采纳

如何查看Go程序测试代码的堆栈跟踪?

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?

图片转代码服务由CSDN问答提供 功能建议

我使用Go的本机测试工具( go test )编写测试。 但是当由于测试代码中的错误导致测试失败时,由于缺少堆栈跟踪或任何其他上下文信息,我真的无法调试它。

甚至,测试代码也需要 一个上下文对象 t ,所以在正常模式下运行测试代码不是简单的工作。

调试测试代码的最佳实践是什么?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • douzong6649 2013-11-03 20:31
    已采纳

    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.

    打赏 评论
  • doukou4066 2013-11-03 22:14

    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

    打赏 评论
  • dpgui8229808 2019-02-19 18:31

    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.

    打赏 评论

相关推荐 更多相似问题