获取Google Test异常抛出信息

11

我的项目中使用了Google Test框架。我正在从代码中抛出异常:

throw DerivedClassException("message");  

并在测试框架中使用如下:

ASSERT_THROW(commond(), DerivedClassException);  
我想要使用what() API获取消息。 是否有方法可以获取异常的确切消息?
1个回答

10

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

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接