obsolete_init_func.html 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
  4. <title>The obsolete initialization function</title>
  5. <link rel="stylesheet" href="../../boostbook.css" type="text/css">
  6. <meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
  7. <link rel="home" href="../../index.html" title="Boost.Test">
  8. <link rel="up" href="../adv_scenarios.html" title="Advanced Usage Scenarios">
  9. <link rel="prev" href="external_test_runner.html" title="The external test runner usage variant">
  10. <link rel="next" href="../practical_usage_recommendations.html" title="Practical usage recommendations">
  11. </head>
  12. <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
  13. <table cellpadding="2" width="100%"><tr>
  14. <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td>
  15. <td align="center"><a href="../../../../../../index.html">Home</a></td>
  16. <td align="center"><a href="../../../../../../libs/libraries.htm">Libraries</a></td>
  17. <td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
  18. <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
  19. <td align="center"><a href="../../../../../../more/index.htm">More</a></td>
  20. </tr></table>
  21. <hr>
  22. <div class="spirit-nav">
  23. <a accesskey="p" href="external_test_runner.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../adv_scenarios.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="../practical_usage_recommendations.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
  24. </div>
  25. <div class="section">
  26. <div class="titlepage"><div><div><h3 class="title">
  27. <a name="boost_test.adv_scenarios.obsolete_init_func"></a><a class="link" href="obsolete_init_func.html" title="The obsolete initialization function">The obsolete
  28. initialization function</a>
  29. </h3></div></div></div>
  30. <p>
  31. For backwards compatibility, the <span class="emphasis"><em>Unit Test Framework</em></span>
  32. also allows the customization of an initialization function of a different
  33. type. This is called the <span class="emphasis"><em>obsolete initialization function</em></span>.
  34. Its signature is:
  35. </p>
  36. <pre class="programlisting"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">unit_test</span><span class="special">::</span><span class="identifier">test_suite</span><span class="special">*</span> <span class="identifier">init_unit_test_suite</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">argc</span><span class="special">,</span> <span class="keyword">char</span><span class="special">*</span> <span class="identifier">argv</span><span class="special">[]);</span>
  37. </pre>
  38. <p>
  39. The original design of the <span class="emphasis"><em>Unit Test Framework</em></span> required
  40. of the programmer to implement it. It was intended to initialize and return
  41. the <a class="link" href="../tests_organization/test_tree/master_test_suite.html" title="Master test suite">master
  42. test suite</a>. No <a class="link" href="../tests_organization/test_cases/test_organization_nullary.html#ref_BOOST_AUTO_TEST_CASE">automatic
  43. test case registration</a> was available at that time. The null-pointer
  44. value was considered an initialization error.
  45. </p>
  46. <p>
  47. In the header-only usage variant, you fall back to the obsolete initialization
  48. function signature by omitting the definition of macro <a class="link" href="../utf_reference/link_references/link_boost_test_alternative_init_macro.html" title="BOOST_TEST_ALTERNATIVE_INIT_API"><code class="computeroutput"><span class="identifier">BOOST_TEST_ALTERNATIVE_INIT_API</span></code></a>
  49. in test module code.
  50. </p>
  51. <h6>
  52. <a name="boost_test.adv_scenarios.obsolete_init_func.h0"></a>
  53. <span class="phrase"><a name="boost_test.adv_scenarios.obsolete_init_func.example_descr"></a></span><a class="link" href="obsolete_init_func.html#boost_test.adv_scenarios.obsolete_init_func.example_descr">Example:
  54. using obsolete initialization function</a>
  55. </h6>
  56. <div class="informaltable"><table class="table">
  57. <colgroup><col></colgroup>
  58. <thead><tr><th>
  59. <p>
  60. Code
  61. </p>
  62. </th></tr></thead>
  63. <tbody><tr><td>
  64. <pre xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="table-programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">test</span><span class="special">/</span><span class="identifier">included</span><span class="special">/</span><span class="identifier">unit_test</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
  65. <span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">iostream</span><span class="special">&gt;</span>
  66. <span class="identifier">BOOST_AUTO_TEST_CASE</span><span class="special">(</span><span class="identifier">test1</span><span class="special">)</span>
  67. <span class="special">{</span>
  68. <span class="identifier">BOOST_TEST</span><span class="special">(</span><span class="keyword">false</span><span class="special">);</span>
  69. <span class="special">}</span>
  70. <span class="identifier">boost</span><span class="special">::</span><span class="identifier">unit_test</span><span class="special">::</span><span class="identifier">test_suite</span><span class="special">*</span> <span class="identifier">init_unit_test_suite</span><span class="special">(</span><span class="keyword">int</span> <span class="comment">/*argc*/</span><span class="special">,</span> <span class="keyword">char</span><span class="special">*</span> <span class="comment">/*argv*/</span><span class="special">[])</span>
  71. <span class="special">{</span>
  72. <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"using obsolete init"</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
  73. <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>
  74. <span class="special">}</span>
  75. </pre>
  76. </td></tr></tbody>
  77. </table></div>
  78. <div class="informaltable"><table class="table">
  79. <colgroup><col></colgroup>
  80. <thead><tr><th>
  81. <p>
  82. Output
  83. </p>
  84. </th></tr></thead>
  85. <tbody><tr><td>
  86. <pre xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="table-programlisting"><span class="special">&gt;</span> <span class="identifier">custom_obsolete_init</span>
  87. <span class="keyword">using</span> <span class="identifier">obsolete</span> <span class="identifier">init</span>
  88. <span class="identifier">Running</span> <span class="number">1</span> <span class="identifier">test</span> <span class="keyword">case</span><span class="special">...</span>
  89. <span class="identifier">test</span><span class="special">.</span><span class="identifier">cpp</span><span class="special">(</span><span class="number">6</span><span class="special">):</span> <span class="identifier">error</span><span class="special">:</span> <span class="identifier">in</span> <span class="string">"test1"</span><span class="special">:</span> <span class="identifier">check</span> <span class="keyword">false</span> <span class="identifier">has</span> <span class="identifier">failed</span>
  90. <span class="special">***</span> <span class="number">1</span> <span class="identifier">failure</span> <span class="identifier">is</span> <span class="identifier">detected</span> <span class="identifier">in</span> <span class="identifier">the</span> <span class="identifier">test</span> <span class="identifier">module</span> <span class="string">"Master Test Suite"</span>
  91. </pre>
  92. </td></tr></tbody>
  93. </table></div>
  94. <p>
  95. In the static-library usage variant, you need to omit the definition of macro
  96. <a class="link" href="../utf_reference/link_references/link_boost_test_alternative_init_macro.html" title="BOOST_TEST_ALTERNATIVE_INIT_API"><code class="computeroutput"><span class="identifier">BOOST_TEST_ALTERNATIVE_INIT_API</span></code></a>
  97. in test module and compile the <span class="emphasis"><em>Unit Test Framework</em></span> static
  98. library without the compilation flag <a class="link" href="../utf_reference/link_references/link_boost_test_alternative_init_macro.html" title="BOOST_TEST_ALTERNATIVE_INIT_API"><code class="computeroutput"><span class="identifier">BOOST_TEST_ALTERNATIVE_INIT_API</span></code></a>
  99. (this is the default).
  100. </p>
  101. <p>
  102. In the shared-library usage variant, it is not possible to use the obsolete
  103. initialization function.
  104. </p>
  105. <p>
  106. Even if you decide to us the obsolete initialization function, it is recommended
  107. that:
  108. </p>
  109. <div class="orderedlist"><ol class="orderedlist" type="1">
  110. <li class="listitem">
  111. You always return a null-pointer value and install the master test suite
  112. via <code class="computeroutput"><a class="link" href="../../boost/unit_test/test_suite.html#idm46279112866240-bb">test_suite::add</a></code> as illustrated <a class="link" href="../tests_organization/test_cases/test_organization_nullary.html#ref_BOOST_TEST_CASE">here</a>. The current framework does
  113. no longer treat the null-pointer value as failure.
  114. </li>
  115. <li class="listitem">
  116. You signal the failure by throwing <code class="computeroutput"><a class="link" href="../../boost/unit_test/framework/setup_error.html" title="Struct setup_error">boost::unit_test::framework::setup_error</a></code>
  117. exception.
  118. </li>
  119. <li class="listitem">
  120. You access the command-line arguments through the interface of the <a class="link" href="../tests_organization/test_tree/master_test_suite.html" title="Master test suite">master
  121. test suite</a>, and ignore the function's arguments <code class="computeroutput"><span class="identifier">argc</span></code> and <code class="computeroutput"><span class="identifier">argv</span></code>.
  122. </li>
  123. </ol></div>
  124. <div class="caution"><table border="0" summary="Caution">
  125. <tr>
  126. <td rowspan="2" align="center" valign="top" width="25"><img alt="[Caution]" src="../../../../../../doc/src/images/caution.png"></td>
  127. <th align="left">Caution</th>
  128. </tr>
  129. <tr><td align="left" valign="top"><p>
  130. The obsolete initialization function is deprecated as its name indicates.
  131. It is recommended to migrate to the new API, and rely on the automated
  132. test unit registration and <a class="link" href="../tests_organization/fixtures.html" title="Fixtures">fixtures</a>
  133. (including <a class="link" href="../tests_organization/fixtures/global.html" title="Global fixture">global
  134. fixtures</a>) for other set-up.
  135. </p></td></tr>
  136. </table></div>
  137. </div>
  138. <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
  139. <td align="left"></td>
  140. <td align="right"><div class="copyright-footer">Copyright &#169; 2001-2019 Boost.Test
  141. contributors<p>
  142. Distributed under the Boost Software License, Version 1.0. (See accompanying
  143. file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
  144. </p>
  145. </div></td>
  146. </tr></table>
  147. <hr>
  148. <div class="spirit-nav">
  149. <a accesskey="p" href="external_test_runner.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../adv_scenarios.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="../practical_usage_recommendations.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
  150. </div>
  151. </body>
  152. </html>