Search code examples
c++includeboost-test

problem with boost.test with old version


I'm trying to use boost.test on a remote system with boost 1.33.1. On my pc this little example from http://www.boost.org/doc/libs/1_42_0/libs/test/doc/html/tutorials/hello-the-testing-world.html works:

#define BOOST_TEST_MODULE MyTest
#include <boost/test/included/unit_test.hpp> // I've changed here

int add( int i, int j ) { return i+j; }

BOOST_AUTO_TEST_CASE( my_test )   // <--- line 7
{
// seven ways to detect and report the same error:
BOOST_CHECK( add( 2,2 ) == 4 );        // #1 continues on error

BOOST_REQUIRE( add( 2,2 ) == 4 );      // #2 throws on error

if( add( 2,2 ) != 4 )
  BOOST_ERROR( "Ouch..." );            // #3 continues on error

if( add( 2,2 ) != 4 )
  BOOST_FAIL( "Ouch..." );             // #4 throws on error

if( add( 2,2 ) != 4 ) throw "Ouch..."; // #5 throws on error

BOOST_CHECK_MESSAGE( add( 2,2 ) == 4,  // #6 continues on error
                     "add(..) result: " << add( 2,2 ) );

BOOST_CHECK_EQUAL( add( 2,2 ), 4 );   // #7 continues on error
}

but on the remote system the file unit_test.hpp doesn't exist. On my pc the file unit_test_framework.hpp is simply:

// deprecated
#include <boost/test/included/unit_test.hpp>

and it is present on the main system. So I tried to change the include to:

#include <boost/test/included/unit_test_framework.hpp>

but the compiler says:

main.cpp:7: error: expected constructor, destructor, or type conversion before ‘(’ token

what's this? How to solve it?


Solution

  • On Boost 1.33 use:

    #include <boost/test/auto_unit_test.hpp>
    

    in place of:

    #include <boost/test/unit_test.hpp>
    

    and also before the #include add:

    #define BOOST_AUTO_TEST_MAIN
    

    or you'll get a linker error