Boost logo

Boost Users :

From: Gennadiy Rozental (rogeeff_at_[hidden])
Date: 2007-09-11 12:55:14


"Chris Miceli" <cmicel1_at_[hidden]> wrote in message
news:46E6AE74.6010101_at_cct.lsu.edu...
>I have had nothing but troubles with boost test 1.34.1 after switched
> from 1.33. First I had trouble with the main from shared object issue,
> and now my code that worked before is not executing a test added to the
> framework.
>
> #include <iostream>
> #define BOOST_TEST_DYN_LINK
> #define BOOST_TEST_MAIN
> #define BOOST_TEST_MODULE File Test
> #include <boost/bind.hpp>
> #include <boost/test/unit_test.hpp>
>
> template <typename Functor>
> struct my_test
> {
> void execute(int value)
> {
> //Never prints this message
> //Doesn't have to be a print, nothing gets here
> std::cerr << "Hi all" << std::endl << std::flush;
> }
> };
>
>
> BOOST_AUTO_TEST_CASE( test123 )
> {
> boost::shared_ptr<my_test<int> > tester(new my_test<int>);
>
> boost::unit_test::framework::master_test_suite().add(BOOST_TEST_CASE(boost::bind(&my_test<int>::execute,
> tester, 5)));
> }

This is incorrect way to register test units.

>
> this worked when it was not using boost::bind but just the address of a
> normal function. This is just an simple version that illustrated the
> problem. I am compiling as such
> g++ test.cpp -o test -L/usr/local/lib -lboost_unit_test_framework-gcc41-mt

I am not sure how it worked before, but the correct way to implement this is
to use init function

#include <iostream>
#define BOOST_TEST_DYN_LINK
#include <boost/bind.hpp>
#include <boost/test/unit_test.hpp>

template <typename Functor>
struct my_test
{
   void execute(int value)
   {
       //Never prints this message
       //Doesn't have to be a print, nothing gets here
       std::cerr << "Hi all" << std::endl << std::flush;
   }
};

bool init_function()
{
  boost::shared_ptr<my_test<int> > tester(new my_test<int>);

  boost::unit_test::framework::master_test_suite().add(
   BOOST_TEST_CASE(boost::bind(&my_test<int>::execute, tester, 5)));

  return true;
}

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

Gennadiy

P.S. Alternatively, if you are not that interrested in either template or
runtime parameter, you can use one of the other automated tools. For
example:

#include <iostream>
#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE Your test
#include <boost/test/unit_test.hpp>

typedef boost::mpl::list<int,long,unsigned char> test_types;

BOOST_AUTO_TEST_CASE_TEMPLATE( test_case1, T, test_types )
{
  //Never prints this message
  //Doesn't have to be a print, nothing gets here
  std::cerr << "Hi all" << std::endl << std::flush;
}


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