简体   繁体   English

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

[英]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.

相关问题 使用EPIC调试器:如何在程序不正确退出时看到有意义的堆栈跟踪 - Using EPIC debugger: how to see get a meaningful stack trace when program exits improperly 如何查看我的程序堆栈跟踪? - How to see my programs stack trace? 如何在 Java 程序中禁用堆栈跟踪生成? - How to disable stack trace generation in a java program? 如何在 Octave 的调试模式下查看堆栈跟踪 - How can I see the stack trace in Octave's debug mode 如何扩展Java堆栈跟踪的大小以查看堆栈的底部? (触发堆栈溢出) - how to expand size of Java stack trace to see bottom of stack? (triggering a stack overflow) 如何获得异常的完整C#堆栈跟踪,就像在Visual Studio中看到的那样? - How can I get the full C# stack trace for an exception, like I see in Visual Studio? 当应用因C / C ++异常而崩溃时,如何查看堆栈跟踪? - How to see stack trace when app is crashed by C/C++ exception? 从控制台运行Java应用程序时如何查看完整的异常堆栈跟踪? - How to see full exception stack trace when running java app from console? Visual Studio .NET调试:在断点处使用现有堆栈跟踪中的变量测试新代码块 - Visual Studio .NET Debugging: Test new code block using variables in existing stack trace when on breakpoint 如何在pudb中显示堆栈跟踪? - How to show stack trace in pudb?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM