简体   繁体   English

不知道使用google-test抛出异常的地方

[英]Don't know where exception was thrown using google-test

We are using Google Test as our C++ unit testing framework. 我们使用Google Test作为我们的C ++单元测试框架。 But I ran into a painful situation and don't know how to deal with. 但我遇到了一个痛苦的局面,不知道如何应对。

Basically, when there is an uncaught exception in the code, I got the following error message printed in the console and get a FAILED . 基本上,当代码中有未捕获的异常时,我在控制台中打印了以下错误消息并获得FAILED Obviously, the exception is captures by google test. 显然,例外是谷歌测试捕获。 However, I have no information at all where is the exception was throw. 但是,我根本没有任何信息,抛出异常。

unknown file: error: SEH exception with code 0xc000005 thrown in the test body. 未知文件:错误:测试正文中抛出代码为0xc000005的SEH异常。

What I can do is debug and step through the code and I will eventually figure out where the problem is. 我能做的就是调试并逐步完成代码,最终我会找出问题所在。 But this is not very efficient as the project is big. 但由于项目很大,这不是很有效。

I want the debugger to stop at the line of uncaught exception and give me a nice call stack. 我希望调试器停在未捕获的异常行并给我一个很好的调用堆栈。 Is there any settings in google test that I don't know of? 谷歌测试中有任何我不知道的设置吗? Any other work around or suggestions will be very much appreciated. 任何其他工作或建议将非常感谢。

Edit: I am looking for something like the following under Windows 编辑:我在Windows下寻找类似下面的内容 在此输入图像描述

Finally according to the answers, I found this settings for visual studio and everything works as the way I want now :) 最后根据答案,我发现了visual studio的这个设置,一切都按照我现在想要的方式工作:) 在此输入图像描述

At work the approach I use is to run only the failing testcase using gdb like so: 在工作中我使用的方法是使用gdb运行只有失败的测试用例,如下所示:

gdb /path/to/test
catch throw
r --gtest_filter='Test.Testcase' --gmock_verbose=info
bt

With visual studio, I suspect you should be able to start your binary with arguments as above, and set a breakpoint to any throw, then take a look at the backtrace. 使用visual studio,我怀疑你应该能够使用上面的参数启动你的二进制文件,并为任何throw设置一个断点,然后看看backtrace。

An SEH Exception is NOT a C++ exception. SEH异常不是 C ++异常。

It is a windows exception that is throw outside of the standard C++ framework for exception handing (there is a different syntax for catching them). 这是一个Windows异常,它抛出标准C ++框架之外的异常处理(捕获它们的语法不同)。

The best way to find the location is to run this inside DevStudio. 找到位置的最佳方法是在DevStudio中运行。 Its been a while but I am sure DevStudio has an option to break when SEH exception is thrown. 它已经有一段时间但我相信DevStudio可以选择在抛出SEH异常时中断。 Just turn this on and your debugger will stop at the throw point and allow you to debug. 只需打开它,您的调试器将停在抛出点并允许您进行调试。

See: https://msdn.microsoft.com/en-us/library/d14azbfh.aspx 请参阅: https//msdn.microsoft.com/en-us/library/d14azbfh.aspx

As noted by @MatthiasVegh you should pass the name of the test as well so you don't have to run through all the tests. 正如@MatthiasVegh所说,您应该传递测试的名称,这样您就不必完成所有测试。

That is not possible since C++ doesn't keep stack trace in the exception object and even if gtest had some smart catching mechanism it would have no means to know where the exception came from. 这是不可能的,因为C ++不会在异常对象中保留堆栈跟踪,即使gtest有一些智能捕获机制,它也无法知道异常的来源。 The best you can do is to store some information in the exception yourself and check it in the test case. 您可以做的最好的事情是自己在异常中存储一些信息,并在测试用例中进行检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM