Boost logo

Boost :

Subject: Re: [boost] Exceptions vs Assertion for library API?
From: Peter Dimov (lists_at_[hidden])
Date: 2017-11-11 11:47:30


Jonathan Biggar wrote:

> I'd like to have the library fail an assertion, which is far clearer to
> the user that the code logic is incorrect, but it bothers me that
> assertions are much more difficult to write unit tests for. (Is there even
> a concept of a test that passes if if fails to compile or fails with an
> assertion in the boost testing framework?)

Easiest is probably to just use a run-fail test. A failed assertion exits
the process with a nonzero code.

Or, you can define BOOST_ENABLE_ASSERT_HANDLER and do whatever you like in
your assertion handler, including throwing an exception that you can then
test with BOOST_TEST_THROWS, but this requires your functions to not be
noexcept. If they are, you either need to conditionally remove the noexcept
from them when some "test mode" macro is defined, or do something like

#include <boost/config.hpp>
#undef BOOST_NOEXCEPT
#define BOOST_NOEXCEPT
#include <boost/mylib/myheader.hpp>

References:

http://www.boost.org/doc/libs/1_65_1/libs/assert/doc/html/assert.html
http://www.boost.org/doc/libs/1_65_1/libs/core/doc/html/core/lightweight_test.html#core.lightweight_test.header_boost_core_lightweight_te.boost_test_throws


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk