EXPECT_THROW
, EXPECT_NO_THROW
, etc. are really part of Google Test rather than Google Mock.
I don't know of any way to get further info on the exception other than hacking the gtest source. For std::exception
s only, the following change should at least output the exception's what()
when an EXPECT_NO_THROW
or ASSERT_NO_THROW
fails.
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.")
You can obviously add more functionality here; catching custom exception types, formatting the failure message to include the exception info, etc.