Test a specific exception type is thrown AND the exception has the right properties

前端 未结 10 1701
梦如初夏
梦如初夏 2020-12-24 01:55

I want to test that MyException is thrown in a certain case. EXPECT_THROW is good here. But I also want to check the exception has a specific state

10条回答
  •  不知归路
    2020-12-24 02:25

    You can try Boost lightweight test:

    #include 
    #include 
    
    void function_that_would_throw(int x)
    {
      if (x > 0) {
        throw std::runtime_error("throw!");
      }
    }
    
    int main() {
     BOOST_TEST_THROWS(function_that_would_throw(10), std::runtime_error);
     boost::report_errors();
    }
    

提交回复
热议问题