Check exception messages using GoogleTest

Is it possible to check the message generated by the exception? Currently, you can do:

ASSERT_THROW(statement, exception_type) 

which is all good and good, but no where can I find a way to check e.what () is really what I'm looking for. Is this impossible to do with the Google test?

+7
c ++ exception-handling googletest
source share
1 answer

Something like the following will work. Just catch the exception somehow, and then do EXPECT_STREQ on calling what() :

 #include "gtest/gtest.h" #include <exception> class myexception: public std::exception { virtual const char* what() const throw() { return "My exception happened"; } } myex; TEST(except, what) { try { throw myex; } catch (std::exception& ex) { EXPECT_STREQ("My exception happened", ex.what()); } } int main(int argc, char **argv) { ::testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); } 
+2
source share

All Articles