12

I run this piece of code

#define BOOST_TEST_MAIN
#define BOOST_TEST_DYN_LINK

#include <boost/test/unit_test.hpp>
#include <boost/test/unit_test_log.hpp>
#include <boost/filesystem/fstream.hpp>

#include <iostream>

using namespace boost::unit_test;
using namespace std;


void TestFoo()
{
    BOOST_CHECK(0==0);
}

test_suite* init_unit_test_suite( int argc, char* argv[] )
{
    std::cout << "Enter init_unit_test_suite" << endl;
    boost::unit_test::test_suite* master_test_suite = 
                        BOOST_TEST_SUITE( "MasterTestSuite" );
    master_test_suite->add(BOOST_TEST_CASE(&TestFoo));
    return master_test_suite;

}

But at runtime it says

Test setup error: test tree is empty

Why does it not run the init_unit_test_suite function?

TemplateRex
  • 69,038
  • 19
  • 164
  • 304
Sam
  • 19,708
  • 4
  • 59
  • 82

2 Answers2

3

Did you actually dynamically link against the boost_unit_test framework library? Furthermore, the combination of manual test registration and the definition of BOOST_TEST_MAIN does not work. The dynamic library requires slightly different initialization routines.

The easiest way to avoid this hurdle is to use automatic test registration

#define BOOST_TEST_MAIN
#define BOOST_TEST_DYN_LINK

#include <boost/test/unit_test.hpp>
#include <boost/test/unit_test_log.hpp>
#include <boost/filesystem/fstream.hpp>

#include <iostream>

using namespace boost::unit_test;
using namespace std;

BOOST_AUTO_TEST_SUITE(MasterSuite)

BOOST_AUTO_TEST_CASE(TestFoo)
{
    BOOST_CHECK(0==0);
}

BOOST_AUTO_TEST_SUITE_END()

This is more robust and scales much better when you add more and more tests.

TemplateRex
  • 69,038
  • 19
  • 164
  • 304
  • Can you be more specific than "the combination of manual test registration and the definition of BOOST_TEST_MAIN does not work". I have the same problem, but would like to use manual test registration. Is this possible? – toftis Feb 12 '14 at 12:51
  • 1
    @user867377 I don't know, I avoid manual registration as the plague because it is very tedious. You could ask a new question. – TemplateRex Feb 12 '14 at 12:57
  • http://stackoverflow.com/questions/21725874/boost-unittest-framework-with-dynamic-linking-and-manual-setup – toftis Feb 12 '14 at 13:02
3

I had exactly the same issue. Besides switching to automatic test registration, as suggested previously, you can also use static linking, i.e. by replacing

#define BOOST_TEST_DYN_LINK

with

#define BOOST_TEST_STATIC_LINK

This was suggested at the boost mailing list:

The easiest way to fix this is to [...] link with static library.

Dynamic library init API is slightly different since 1.34.1 and this is the cause of the error you see. init_unit_test_suite function is not called in this case.

Community
  • 1
  • 1
Sebastian
  • 8,046
  • 2
  • 34
  • 58