开发者

Print exception.what() in Google Test

some of my code throws using

if (failure)
  throw std::runtime_error("a bad thing happened: ...");

I am using Google Test and TeamCity to automatically execute my tests. It's running on Windows, so I use the --gtest_catch_exceptions parameter to report a test as failed if an unexpected exception happens. However, Google Test simply fails the test with a message like

Exception thrown with code 0xe06d7363 in the test body.
in (null) line -1

which is not very helpful. I'd rather have a message like

Exception thrown: "a bad thing happened: ..."

I have a custom TestListener which implements the method

OnTestPartResult( const ::testing::TestPartResult& test_part_result)

but it seems like there is no reference to the exception that was caught by Google Test. Is there any other way to r开发者_Python百科eport the exception to std::cout or somewhere else?

Please note that I cannot use

try
{
    return RUN_ALL_TESTS();
}
catch (std::exception& e)
{
    std::cout << "EXCEPTION: " << e.what();
    return -1;
}
catch (...)
{
    return -1;
}

without --gtest_catch_exceptions, because test execution then gets "cancelled" on the first exception.

I also would not like to change the throwing code.

Thanks for any ideas!


I am using the gtest provided by gmock-1.7.0. Here is what I did from the gmock-1.7.0 directory:

diff --git a/gtest/include/gtest/internal/gtest-internal.h b/gtest/include/gtest/internal/gtest-internal.h
index 0dcc3a3..265093b 100644
--- a/gtest/include/gtest/internal/gtest-internal.h
+++ b/gtest/include/gtest/internal/gtest-internal.h
@@ -1075,7 +1075,8 @@ class NativeArray {
     try { \
       GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \
     } \
-    catch (...) { \
+    catch (std::exception *e ) { \
+       std::cout << e->what() << std::endl;  \
       goto GTEST_CONCAT_TOKEN_(gtest_label_testnothrow_, __LINE__); \
     } \
   } else \

In English, I explicitly caught std::exception instead of .. (everything I throw is derived from that), and added an echo of the e->what()

Hope this helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜