Search code examples
c++unit-testingboostassertboost-test

Testing for assert in the Boost Test framework


I use the Boost Test framework to unit test my C++ code and wondered if it is possible to test if a function will assert? Yes, sounds a bit strange but bear with me! Many of my functions check the input parameters upon entry, asserting if they are invalid, and it would be useful to test for this. For example:

void MyFunction(int param)
{
    assert(param > 0); // param cannot be less than 1
    ...
}

I would like to be able to do something like this:

BOOST_CHECK_ASSERT(MyFunction(0), true);
BOOST_CHECK_ASSERT(MyFunction(-1), true);
BOOST_CHECK_ASSERT(MyFunction(1), false);
...

You can check for exceptions being thrown using Boost Test so I wondered if there was some assert magic too...


Solution

  • I don't think so. You could always write your own assert which throws an exception and then use BOOST_CHECK_NOTHROW() for that exception.