boostorg / test

The reference C++ unit testing framework (TDD, xUnit, C++03/11/14/17)
http://boost.org/libs/test
Boost Software License 1.0
183 stars 140 forks source link

Support checking exception type and message #338

Open teeks99 opened 2 years ago

teeks99 commented 2 years ago

For some basic code:


#include <stdexcept>

class MyException : public std::runtime_error
{
public:
   MyException(std::string cause)
      : std::runtime_error(cause)
   {}
};

void MyCode()
{
   throw MyException("A very specific message");
}

I have a pattern that is very frequently used:

BOOST_AUTO_TEST_CASE(CheckCorrectException)
{
   std::string expectedMessage = "A very specific message";

   BOOST_CHECK_EXCEPTION(MyCode(), MyException, [expectedMessage](const MyException& ex) -> bool
   {
       BOOST_CHECK_EQUAL(ex.what(), expectedMessage);
       return true;
   });
}

I was wondering if this pattern of checking they type and message could be common enough to make another check that explicitly checks the type and message.

e.g. BOOST_CHECK_EXCEPTION_MESSAGE(statement, exception, message)