Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boost test: catch user defined exceptions

If I have user defined exceptions in my code, I can't get Boost test to consider them as failures.

For example,

BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(MyTest,1)
BOOST_AUTO_TEST_CASE(MyTest)
{
// code which throws user defined exception, not derived from std::exception.

}

I get a generic message:

Caught exception: ....
unknown location(0):....

It does not recognize this error as a failure since it is not a std::exception. So it does not honor the expected_failures clause.

How do I enforce that the piece of code should always throw an exception? THis seems to be a useful thing to want. In case future code changes cause the code to pass and the exception is not thrown, I want to know that.

like image 696
user231536 Avatar asked Feb 27 '23 23:02

user231536


1 Answers

The EXPECTED_FAILURES are referring to failures against BOOST_REQUIRE or other assertions. The documentation clearly states:

The feature is not intended to be used to check for expected functionality failures. To check that a particular input is causing an exception to be thrown use BOOST_CHECK_THROW family of testing tools.

Emphasis was mine.

The expected failures are meant to be used as a temporary workaround during testing when an assertion is failing but you want to temporarily ignore it.

Taking a snippet from their expected failures spec:

BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES( my_test1, 1 )

BOOST_AUTO_TEST_CASE( my_test1 )
{
    BOOST_CHECK( 2 == 1 );
}

will result in output of

test.cpp(10): error in "my_test1": check 2 == 1 failed

Test suite "example" passed with:
  1 assertions out of 1 failed
  1 failures expected
  1 test case out of 1 passed

As you can see, in spite of assertions failing, the test case still passed due to the use of expected failures.


So if you need to verify that something is throwing an exception, you use code like the following:

BOOST_AUTO_TEST_CASE(invalid_operation_should_throw_custom_exception)
{
    MyObj obj;
    BOOST_REQUIRE_THROW(obj.invalid_operation(), CustomException);
}
like image 192
Mark Rushakoff Avatar answered Mar 07 '23 01:03

Mark Rushakoff