Print exception.what () in Google Test

some of my code throws using

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

I use Google Test and TeamCity to automatically run my tests. It works on Windows, so I use the -gtest_catch_exceptions option to report that the test was unsuccessful if an unexpected exception occurred. However, Google Test just fails when testing with a message like

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

which is not very useful. I would rather have a message like

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

I have a custom TestListener that implements a method

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

but it looks like there is no reference to the exception that was detected in Google Test. Is there any other way to report an exception from 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 the test execution is then "canceled" in the first exception.

I would also not like to change the throwing code.

Thanks for any ideas!

+4
source share
1 answer

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

 diff --git a/gtest/include/gtest/internal/gtest-internal.hb/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 do comes from this) and added an echo e> what ()

Hope this helps.

0
source

Source: https://habr.com/ru/post/1335048/


All Articles