EXPECT_THROW
, EXPECT_NO_THROW
, etc. really are part of Google Test , not Google Mock.
I do not know how to get additional information about the exception, except for hacking the gtest source. For std::exception
only the next change should at least throw a what()
exception when EXPECT_NO_THROW
or ASSERT_NO_THROW
.
In gtest / include / gtest / internal / gtest-internal.h, around line 1140, change the GTEST_TEST_NO_THROW_
macro to:
#define GTEST_TEST_NO_THROW_(statement, fail) \ GTEST_AMBIGUOUS_ELSE_BLOCKER_ \ if (::testing::internal::AlwaysTrue()) { \ try { \ GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \ } \ catch (...) { \ try { \ std::exception_ptr exceptn_ptr(std::current_exception()); \ std::rethrow_exception(exceptn_ptr); \ } catch(const std::exception& exceptn) { \ std::cerr << exceptn.what() << '\n'; \ } \ goto GTEST_CONCAT_TOKEN_(gtest_label_testnothrow_, __LINE__); \ } \ } else \ GTEST_CONCAT_TOKEN_(gtest_label_testnothrow_, __LINE__): \ fail("Expected: " #statement " doesn't throw an exception.\n" \ " Actual: it throws.")
Obviously, you can add more features; catching custom exception types, formatting error messages to include exception information, etc.
source share