【问题标题】:Get google test exception throw message [duplicate]获取谷歌测试异常抛出消息[重复]
【发布时间】:2015-04-08 05:11:42
【问题描述】:

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

throw DerivedClassException("message");  

并在测试框架中使用:

ASSERT_THROW(commond(), DerivedClassException);  

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

【问题讨论】:

    标签: c++ c++11 exception-handling googletest


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-06-22
      • 2012-11-19
      • 2020-12-04
      • 2018-12-10
      • 1970-01-01
      • 2023-03-08
      • 2014-09-09
      相关资源
      最近更新 更多