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

拟墨画扇 提交于 2019-12-05 08:56:26

问题


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.


回答1:


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




回答2:


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



来源:https://stackoverflow.com/questions/172854/how-do-you-specify-that-an-exception-should-be-expected-using-boost-test

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!