从ASSERT_THROW获取异常消息
Getting exception message from ASSERT_THROW
我正在使用googletest
,在我的许多测试中,我都使用ASSERT_THROW
命令。问题是,例如,如果它抛出的异常与我预期的异常不同,我得到的只是:
实际:它抛出了一个不同的类型。
有没有办法让它吐出what()
什么的返回值?
你可能通过指针抛出(使用new
关键字)
throw new MyDerivedException();
并预期会收到非指针类型的异常:
EXPECT_THROW(blah, MyDerivedException);
你应该按值抛出。
throw MyDerivedException(); // notice lack of 'new'
此功能
在 1.11 版googletest
中可用。( https://github.com/google/googletest/pull/2903 )
对我有用的(也许是幼稚的)解决方案是定义以下宏:
#define ASSERT_THROW_WITH_MESSAGE(code, expected_exception, expected_message) do {
try {
{ code; }
FAIL() << "no exception occured" << endl;
}
catch (const expected_exception &e) {
EXPECT_THAT(e.what(), testing::HasSubstr(expected_message));
}
catch (const std::exception &e) {
FAIL() << "an unexpected exception occured: " << e.what() << endl;
}
catch (...) {
FAIL() << "an unknown exception occured" << endl;
}
} while(0);
抛出新的MyDerivedException() ...EXPECT_THROW(废话,我的衍生例外*);
相关文章:
- 从函数(不抛出函数)返回异常以进行消息格式化?
- C++MSVC-显示未捕获的异常消息
- 奇怪的消息 (_Base_bitset::_M_do_to_ulong) 从溢出异常处理程序中打印出来
- 从处理程序捕获时如何获取 Ada 异常消息C++?
- 核心消息传递中未处理的异常.dll在程序关闭期间
- C++未显示自定义异常消息
- 自定义异常中的消息已损坏
- 异常消息为空
- 捕获加速异常并提取其消息
- 运行时异常消息而不扩展 std::exception
- 异常是否应具有标志/消息,或者每个错误应具有异常类型
- 获取谷歌测试异常抛出消息
- 从ASSERT_THROW获取异常消息
- 使用 GoogleTest 验证异常消息
- 如何与调试器通信或更改异常消息
- 是由通过互操作传递到C++的C#组件引发的异常消息
- 异常消息:插入错误值的字符串表示
- 如何将异常消息从gsoap发送到std::字符串?
- BOOST_CHECK_NO_THROW如何打印异常消息
- Boost::Asio异步UDP服务器-未处理的异常消息