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>
#include <boost/mylib/myheader.hpp>


Boost list run by bdawes at, gregod at, cpdaniel at, john at