Boost C++ Libraries

...one of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

libs/test/doc/adv_scenarios/shared_lib_customizations.qbk

[/
 / Copyright (c) 2003 Boost.Test contributors
 /
 / Distributed under the Boost Software License, Version 1.0. (See accompanying
 / file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
 /]

[section:shared_lib_customizations Shared-library variant customizations]

[caution Macro __BOOST_TEST_DYN_LINK__ (which instructs the compiler/linker to dynamically link against a shared
library variant) may be implicitly defined when macro `BOOST_ALL_DYN_LINK` is defined.]

[caution In order to be able to run a test built with the dynamic variant, the operating system should be able
 to find the dynamic library of the __UTF__. This means, for example on Linux and MacOSX respectively, setting the environment
 variable `LD_LIBRARY_PATH` or `DYLD_LIBRARY_PATH` properly prior to the execution of the test module.]

[section:entry_point Customizing the module's entry point]

In this variant, in one of the source files, you now have to define your custom entry point, and invoke the default
[link boost_test.adv_scenarios.test_module_runner_overview test runner] `unit_test_main` manually with the default
[link boost_test.adv_scenarios.test_module_init_overview initialization function] `init_unit_test` as argument.
You need to define __BOOST_TEST_NO_MAIN__ (its value is irrelevant) in the main file:

[table
[[In *exactly one* file][In all other files]]
[[```#define BOOST_TEST_MODULE test module name
#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_NO_MAIN
#include <boost/test/unit_test.hpp>

// entry point:
int main(int argc, char* argv[], char* envp[])
{
  return boost::unit_test::unit_test_main( &init_unit_test, argc, argv );
}
```]
[```#define BOOST_TEST_DYN_LINK
#include <boost/test/unit_test.hpp>

//
// test cases
//

//
// test cases
//
```]]
]

[endsect] [/section:entry_point]

[section:init_func Customizing the module's initialization function]

In the shared-library variant, it is impossible to customize the initialization function without
[link boost_test.adv_scenarios.shared_lib_customizations.entry_point customizing the entry point]. We have
to customize both. In one of the source files, you now have to define your custom entry point and
[link boost_test.adv_scenarios.test_module_init_overview initialization function] `init_unit_test`; next invoke
the default [link boost_test.adv_scenarios.test_module_runner_overview test runner] `unit_test_main` manually
with `init_unit_test` as argument. You ['do not] define __BOOST_TEST_MODULE__ in the main file:

[table
[[In *exactly one* file][In all other files]]
[[```#define BOOST_TEST_DYN_LINK
#include <boost/test/unit_test.hpp>

// initialization function:
bool init_unit_test()
{
  return true;
}

// entry point:
int main(int argc, char* argv[])
{
  return boost::unit_test::unit_test_main( &init_unit_test, argc, argv );
}
```]
[```#define BOOST_TEST_DYN_LINK
#include <boost/test/unit_test.hpp>

//
// test cases
//

//
// test cases
//

//
// test cases
//
```]]
]

For reporting errors that may occur during the initialization,

* either you return `false` (valid only for the new API only, see __BOOST_TEST_ALTERNATIVE_INIT_API__)
* or you raise an exception such as `std::runtime_error` or [classref boost::unit_test::framework::setup_error]

An error reported in this function aborts the execution of the test module.

[endsect] [/section:init_func]

[endsect] [/section:shared_lib_customizations]