Boost logo

Boost Users :

From: Ovanes Markarian (om_boost_at_[hidden])
Date: 2008-05-01 16:59:38


Please see my answers below.

On Thu, May 1, 2008 at 9:33 PM, Robert Dailey <rcdailey_at_[hidden]> wrote:

> Hey guys,
>
> I'm currently in the process of researching Boost.Test. I've never used it
> before, and reading through the documentation for it and the various
> examples, I have a few questions I can't find the answers to. I'll post them
> below:
>
> 1. init_unit_test_suite is used fairly exclusively through the
> examples. The most I can gather is that this function begins the unit test.
> In other words, it calls other smaller functions which perform tests. How is
> this function evoked?
>
> This function is invoked by the unit test framework internally.

>
> 1.
> 2. What is the difference in using init_unit_test_suite versus using
> #define BOOST_TEST_MAIN?
>
> Using init_unit_test_suite you have to manually register all your test
cases to be excuted. Using BOOST_TEST_MAIN and BOOST_TEST_CASE (or whatever
was the name of the macro) the framework will handle it for you. You just
write test cases and are no longer required to care about main() and test
case registration.

>
> 1.
> 2. When creating a unit test as a console application in Visual
> Studio, I would of course be required to implement the normal int
> main() function, but what would I have to do inside of main() or
> WinMain() to begin the unit test? I never found any examples that
> showed this.
>
> As already stated these function are implemented by the test framework
which links with your code and compiler finds them. The main drawback using
init_unit_test_suite is when you are not allowed to use a static version of
the boost test lib, so that DLL links to your code. In this case main and so
on are required to be implemented by you, where in case of BOOST_TEST_MAIN
it is automatically generated by the macro.

I initially used init_unit_test_suite to be able to create test hierarchies
but after switching to the dynamic boost lib (due to project requirements) I
was forced to reimplement the init_unit_test_suite. It was not difficult,
but I had to find how to do it, which was not easy.

This is the code I used:

Initialize BOOST Unit Test, when it is used as DLL and custom main function
is required

#include <boost/test/unit_test.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/ref.hpp>

// create this post-build event
//Autorun Unit Tests
//"$(TargetDir)\$(TargetName).exe" --result_code=no --report_level=no

namespace ut=boost::unit_test;

bool *init_test_suite*()
{
    using namespace boost;
    using namespace ut;

  master_test_suite_t& master=framework::master_test_suite();

  test_suite* test= BOOST_TEST_SUITE( "Testing configuration parameter
library" );
  master.add(test);

        test_suite* plaf = BOOST_TEST_SUITE( "Testing Platform Services
implementation" );
        test->add(plaf);
            shared_ptr<plaf_svc_test> platform_tc( new
plaf_svc_test(master.argv[0]) );
            plaf->add( BOOST_CLASS_TEST_CASE(&plaf_svc_test::test_name,
platform_tc) );
            plaf->add( BOOST_CLASS_TEST_CASE(&plaf_svc_test::test_path,
platform_tc) );

        test_suite* conf_gen = BOOST_TEST_SUITE( "Testing Configuration File
generation" );
        test->add(conf_gen);
            shared_ptr<config_gen_tests> cfg_tc( new config_gen_tests );
            conf_gen->add(
BOOST_CLASS_TEST_CASE(&config_gen_tests::test_generated_types, cfg_tc) );
            conf_gen->add(
BOOST_CLASS_TEST_CASE(&config_gen_tests::test_valid_names, cfg_tc) );
            conf_gen->add(
BOOST_CLASS_TEST_CASE(&config_gen_tests::test_invalid_names, cfg_tc) );

        test_suite* parsing = BOOST_TEST_SUITE( "Config File Parsing Related
Tests" );
        test->add(parsing);
            parsing->add(BOOST_TEST_CASE(&cfg_parsing_tests::start_tests));

    return true;
}

int main(int argc, char* argv[])
{
  return ::boost::unit_test::*unit_test_main*(&*init_test_suite*, argc,
argv);
}

> 1.
>
> If I think of more questions I'll be sure to post follow ups. Help is
> greatly appreciated. Thanks in advance!
>
> Hope that helps,
Ovanes



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