Boost logo

Boost :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2007-02-25 12:00:20


David Abrahams wrote:
> I just did a sweep through all our code and found and fixed lots of
> issues related to the use of BOOST_TEST macro, all of which boiled
> down to the fact that it is possible to use it and never call
> boost::report_errors(). In several places it had been used as a
> drop-in replacement for assert(), which it isn't because failure to
> call report_errors will allow test failures to pass silently. It was
> also used in a couple places in lieu of BOOST_CHECK, i.e. the author
> obviously was trying to use Boost.Test but got the wrong macro name.
>
> I suggest we do something to make it harder to make that mistake. The
> only approach I can think of is to create an object at namespace scope
> whose destructor checks to see if we're exiting with a nonzero error
> count and without having called report_errors. Or maybe it should
> just do something like:
>
> namespace boost { namespace detail {
> template <bool = false>
> struct lightweight_error_reporter
> {
> ~lightweight_error_reporter()
> {
> if (boost::report_errors())
> {
> abort();
> }
> }
>
> static lightweight_error_reporter instance;
> };
> }}
>
>
> Whether abort() or exit(1) or throw xxx is more appropriate there I
> don't know (haven't done the research).

Being the conservative I am, I'd like to keep the current way operational
without altering the behavior of the tests that are written correctly and do
work.

The automatic reporting outlined above always leads to abnormal termination
when there are errors; I don't like this. The alternative that just
terminates when report_errors hasn't been called seems workable.

One consequence of the minimal interface of lightweight_test.hpp is that
main(), including its 'return' statement, is completely under the control of
the user.

It would probably be possible to make it slightly less lightweight and error
prone by defining a BOOST_TEST_CASE macro and requiring a main() of the form

int main()
{
    return boost::run_test_cases();
}

Well, the current way has worked for me so far. :-)


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