Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you specify that an exception should be expected using Boost.Test?

I have a Boost unit test case which causes the object under test to throw an exception (that's the test, to cause an exception). How do I specify in the test to expect that particular exception.

I can specify that the test should have a certain number of failures by using BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES but that seems rather unspecific. I want to be able to say at a specific point in the test that an exception should be thrown and that it should not be counted as a failure.

like image 266
Ferruccio Avatar asked Oct 05 '08 23:10

Ferruccio


People also ask

How do you use boost test?

Create a Boost. cpp file for your tests, right-click on the project node in Solution Explorer and choose Add > New Item. In the Add New Item dialog, expand Installed > Visual C++ > Test. Select Boost. Test, then choose Add to add Test.


2 Answers

Doesn't this work?

BOOST_CHECK_THROW (expression, an_exception_type); 

That should cause the test to pass if the expression throws the given exception type or fail otherwise. If you need a different severity than 'CHECK', you could also use BOOST_WARN_THROW() or BOOST_REQUIRE_THROW() instead. See the documentation

like image 134
jonner Avatar answered Sep 25 '22 20:09

jonner


You can also use BOOST_CHECK_EXCEPTION, which allows you to specify test function which validates your exception.

like image 29
Tomek Avatar answered Sep 22 '22 20:09

Tomek