single_header_customizations.qbk 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. [/
  2. / Copyright (c) 2003 Boost.Test contributors
  3. /
  4. / Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. / file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. /]
  7. [section:single_header_customizations Header-only variant customizations]
  8. [section:multiple_translation_units Header-only with multiple translation units]
  9. It is possible to use the header-only variant of the __UTF__ even if the test module has multiple translation
  10. units:
  11. * one translation unit should define __BOOST_TEST_MODULE__ and include `<boost/test/included/unit_test.hpp>`
  12. * all the other translation units should include `<boost/test/unit_test.hpp>`
  13. An example might be the following:
  14. * Translation unit 1, defines __BOOST_TEST_MODULE__
  15. ```
  16. #define BOOST_TEST_MODULE header-only multiunit test
  17. #include <boost/test/included/unit_test.hpp>
  18. BOOST_AUTO_TEST_CASE( test1 )
  19. {
  20. int i = 1;
  21. BOOST_CHECK( i*i == 1 );
  22. }
  23. ```
  24. * Translation unit 2, includes `<boost/test/unit_test.hpp>` instead of `<boost/test/included/unit_test.hpp>`:
  25. ```
  26. #include <boost/test/unit_test.hpp>
  27. BOOST_AUTO_TEST_CASE( test2 )
  28. {
  29. int i = 1;
  30. BOOST_CHECK( i*i == 1 );
  31. }
  32. ```
  33. [endsect] [/section:multiple_translation_units]
  34. [section:entry_point Customizing the module's entry point]
  35. In this usage variant and in the translation unit containing the definition of __BOOST_TEST_MODULE__,
  36. you need to define the macros __BOOST_TEST_NO_MAIN__ and
  37. __BOOST_TEST_ALTERNATIVE_INIT_API__ (their values are irrelevant) prior to including any of the framework's headers.
  38. Next, you have to define your custom entry point, and invoke the default [link
  39. boost_test.adv_scenarios.test_module_runner_overview test runner] `unit_test_main` manually with the default [link
  40. boost_test.adv_scenarios.test_module_init_overview initialization function] `init_unit_test` as argument.
  41. [bt_example custom_main..using custom entry point..run-fail]
  42. In the above example, a custom entry point was selected because the test module, in addition to command line arguments,
  43. needs to obtain also the information about environment variables.
  44. [note The above example also illustrates that it makes sense to define both __BOOST_TEST_MODULE__ and
  45. __BOOST_TEST_NO_MAIN__. This way, no `main` is generated by the framework, but the name specified by __BOOST_TEST_MODULE__
  46. is assigned to the [link boost_test.tests_organization.test_tree.master_test_suite Master test suite].]
  47. [note The reason for defining __BOOST_TEST_ALTERNATIVE_INIT_API__ is described [link
  48. boost_test.adv_scenarios.obsolete_init_func here].]
  49. [endsect] [/section:entry_point]
  50. [section:init_func Customizing the module's initialization function]
  51. In this usage variant, you do not define macro __BOOST_TEST_MODULE__ and instead provide the definition of function
  52. `init_unit_test`. This is going to be the custom initialization function. The default [link
  53. boost_test.adv_scenarios.test_module_runner_overview test runner] will use it to initialize the test module.
  54. [bt_example custom_init..using custom initialization function..run-fail]
  55. [note Because we overwrote the default initialization function, it does no longer assign any name to the [link
  56. boost_test.tests_organization.test_tree.master_test_suite master test suite]. Therefore the default name ("Master Test
  57. Suite") is used.]
  58. For reporting errors that may occur during the initialization,
  59. * either you return `false` (valid only for the new API only, see __BOOST_TEST_ALTERNATIVE_INIT_API__)
  60. * or you raise an exception such as `std::runtime_error` or [classref boost::unit_test::framework::setup_error]
  61. An error reported in this function aborts the execution of the test module.
  62. [note The reason for defining __BOOST_TEST_ALTERNATIVE_INIT_API__ is described [link
  63. boost_test.adv_scenarios.obsolete_init_func here].]
  64. [endsect] [/section:init_func]
  65. [endsect] [/section:single_header_customizations]