Boost logo

Boost Users :

Subject: Re: [Boost-users] Boost::Test and streams
From: Gennadiy Rozental (rogeeff_at_[hidden])
Date: 2011-08-09 01:11:41


Jeffrey Walton <noloader <at> gmail.com> writes:

> Hi All,
>
> I can't seem to find Boost::Test's streams. The discussion
> of streams begins at [1]. [1] does list macros such as
> BOOST_TEST_DONT_PRINT_LOG_VALUE, but I'm interested in a
> C++-centric solution. For example:

IMHO the solution Boost.Test implements is C++ centric. We'll
have to agree to disagree here.
 
> // test framework output stream
> using boost_test::tout;
>
> tout << loglevel::error << "My error message" << endl;
> tout << loglevel::warn << "My warning message" << endl;

Boost.Test takes different approach. You don't actually have test
output stream (well there is a stream, but more on that later) and
you don't actually write print statements. In your case case test
program would look something like:

if( ! my condition )
    tout << loglevel::error << "My error message" << endl;

Boost.Test alternative is

BOOST_CHECK_MESSAGE( my_condition, "My error message" );

This approach is more "functional". There are no if statements.
Your test program is just a collection of assertion statements.
In addition to helping you generate correct message (referencing
failed values) Boost.Test testing tools will make sure that
message will only being generated if you actually plan to
print it.

Warning vs. Error.
==================

All Boost.Test testing tools have 3 "levels". You decide the
severity of the assertion by choosing the right tool level. For example:

BOOST_CHECK_EQUAL - "error" level
BOOST_REQUIRE_LT - "fatal error" level
BOOST_WARNING_GT - "warning" level

> According to [2], I can set the log level from the environment and
> from the command line. But would like to set it dynamically. For
> example:
>
> tout << setlevel(warn); // warnings and above
> or
> tout.setlelvel(error); // errors and above

First of all I'd like to note that this is probably undesirable in
general. The log level (as well as report level) are intended to
allow test module user to change amount of output on request.
Setting it programmatically defeat this purpose. That said you can
use these functions to set level and format of the log:

    unit_test_log.set_threshold_level( log_level l );
    unit_test_log.set_format( output_format f );

and these methods to set level and format of the report:

    results_reporter::set_level( report_level l );
    results_reporter::set_format( output_format f );
 
> In addition, I would like to be able to tie a file output to
> screen output, so I can watch the tests roll by and go to the
> log file for in depth reading. To be fair, redirection is
> covered at [3], but its not quite what we are looking for.
> For example:
>
> ofstream logfile(...);
> ...
>
> tout.attach(logfile);
> or
> tout.tie(logfile);

You can redirect both log and report streams using these functions:

    unit_test_log.set_stream( std::ostream& );
    results_reporter::set_stream( std::ostream& );

If you want anything like multi-directional stream I am sure
boost::iostreams can facilitate that.
 
> Though the authors did a great job on the documentation,

I can smell a sarcasm here.

> these topics
> do not appear to be included. Any ideas how one might go
> about these sorts of things?

I hope my answers here are satisfactory.

Regards,
Gennadiy


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net