如何指定使用Boost.Test应该会有异常? [英] How do you specify that an exception should be expected using Boost.Test?

查看:171
本文介绍了如何指定使用Boost.Test应该会有异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Boost单元测试用例,导致被测对象抛出异常(这是测试引起异常)。如何在测试中指定要预期的特定异常。



我可以通过使用BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES指定测试应该有一定数量的故障,但是这似乎是非特定的。我想能够在测试中的一个特定点说出应该抛出一个异常,并且不应该被视为失败。

解决方案

这不行吗?

  BOOST_CHECK_THROW(expression,an_exception_type); 

如果表达式抛出给定的异常类型或失败,则应该导致测试通过。如果您需要与CHECK不同的严重性,您还可以使用 BOOST_WARN_THROW() BOOST_REQUIRE_THROW()。请参见文档


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.

解决方案

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

这篇关于如何指定使用Boost.Test应该会有异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆