获取谷歌测试异常抛出消息

anu*_*udh 8 c++ exception-handling googletest c++11

我正在为我的项目使用谷歌测试框架.我从代码中抛出异常为:

throw DerivedClassException("message");  
Run Code Online (Sandbox Code Playgroud)

并在测试框架中使用as:

ASSERT_THROW(commond(), DerivedClassException);  
Run Code Online (Sandbox Code Playgroud)

我想通过what()API 获取消息.以任何方式获取异常的确切异常消息.

BЈо*_*вић 8

检查抛出异常的唯一方法是在测试中捕获它:

void test_foo( MyTest, TestException )
{
  try
  {
    functionThatThrowsException();
    FAIL();
  }
  catch( const DerivedClassException& err )
  {
    // check exception
    ASSERT_STREQ( "error message", err.what() );
  }
}
Run Code Online (Sandbox Code Playgroud)