1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 4<title>Customizing the module's 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="../single_header_customizations.html" title="Header-only variant customizations"> 9<link rel="prev" href="entry_point.html" title="Customizing the module's entry point"> 10<link rel="next" href="../static_lib_customizations.html" title="Static-library variant customizations"> 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="entry_point.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../single_header_customizations.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="../static_lib_customizations.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a> 24</div> 25<div class="section"> 26<div class="titlepage"><div><div><h4 class="title"> 27<a name="boost_test.adv_scenarios.single_header_customizations.init_func"></a><a class="link" href="init_func.html" title="Customizing the module's initialization function">Customizing 28 the module's initialization function</a> 29</h4></div></div></div> 30<p> 31 In this usage variant, you do not define macro <a class="link" href="../../utf_reference/link_references/link_boost_test_module_macro.html" title="BOOST_TEST_MODULE"><code class="computeroutput"><span class="identifier">BOOST_TEST_MODULE</span></code></a> and instead 32 provide the definition of function <code class="computeroutput"><span class="identifier">init_unit_test</span></code>. 33 This is going to be the custom initialization function. The default <a class="link" href="../test_module_runner_overview.html" title="Test module runner">test runner</a> 34 will use it to initialize the test module. 35 </p> 36<h6> 37<a name="boost_test.adv_scenarios.single_header_customizations.init_func.h0"></a> 38 <span class="phrase"><a name="boost_test.adv_scenarios.single_header_customizations.init_func.example_descr"></a></span><a class="link" href="init_func.html#boost_test.adv_scenarios.single_header_customizations.init_func.example_descr">Example: 39 using custom initialization function</a> 40 </h6> 41<div class="informaltable"><table class="table"> 42<colgroup><col></colgroup> 43<thead><tr><th> 44 <p> 45 Code 46 </p> 47 </th></tr></thead> 48<tbody><tr><td> 49<pre xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="table-programlisting"><span class="preprocessor">#define</span> <span class="identifier">BOOST_TEST_ALTERNATIVE_INIT_API</span> 50<span class="preprocessor">#include</span> <span class="special"><</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">></span> 51<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">iostream</span><span class="special">></span> 52 53<span class="identifier">BOOST_AUTO_TEST_CASE</span><span class="special">(</span><span class="identifier">test1</span><span class="special">)</span> 54<span class="special">{</span> 55 <span class="identifier">BOOST_TEST</span><span class="special">(</span><span class="keyword">false</span><span class="special">);</span> 56<span class="special">}</span> 57 58<span class="keyword">bool</span> <span class="identifier">init_unit_test</span><span class="special">()</span> 59<span class="special">{</span> 60 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"using custom init"</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span> 61 <span class="keyword">return</span> <span class="keyword">true</span><span class="special">;</span> 62<span class="special">}</span> 63</pre> 64 </td></tr></tbody> 65</table></div> 66<div class="informaltable"><table class="table"> 67<colgroup><col></colgroup> 68<thead><tr><th> 69 <p> 70 Output 71 </p> 72 </th></tr></thead> 73<tbody><tr><td> 74<pre xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="table-programlisting"><span class="special">></span> <span class="identifier">custom_init</span> 75<span class="keyword">using</span> <span class="identifier">custom</span> <span class="identifier">init</span> 76<span class="identifier">Running</span> <span class="number">1</span> <span class="identifier">test</span> <span class="keyword">case</span><span class="special">...</span> 77<span class="identifier">test</span><span class="special">.</span><span class="identifier">cpp</span><span class="special">(</span><span class="number">7</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> 78 79<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> 80</pre> 81 </td></tr></tbody> 82</table></div> 83<div class="note"><table border="0" summary="Note"> 84<tr> 85<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../../../../doc/src/images/note.png"></td> 86<th align="left">Note</th> 87</tr> 88<tr><td align="left" valign="top"><p> 89 Because we overwrote the default initialization function, it does no 90 longer assign any name to the <a class="link" href="../../tests_organization/test_tree/master_test_suite.html" title="Master test suite">master 91 test suite</a>. Therefore the default name ("Master Test Suite") 92 is used. 93 </p></td></tr> 94</table></div> 95<p> 96 For reporting errors that may occur during the initialization, 97 </p> 98<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "> 99<li class="listitem"> 100 either you return <code class="computeroutput"><span class="keyword">false</span></code> 101 (valid only for the new API only, see <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>) 102 </li> 103<li class="listitem"> 104 or you raise an exception such as <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">runtime_error</span></code> 105 or <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> 106 </li> 107</ul></div> 108<p> 109 An error reported in this function aborts the execution of the test module. 110 </p> 111<div class="note"><table border="0" summary="Note"> 112<tr> 113<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../../../../doc/src/images/note.png"></td> 114<th align="left">Note</th> 115</tr> 116<tr><td align="left" valign="top"><p> 117 The reason for defining <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> 118 is described <a class="link" href="../obsolete_init_func.html" title="The obsolete initialization function">here</a>. 119 </p></td></tr> 120</table></div> 121</div> 122<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 123<td align="left"></td> 124<td align="right"><div class="copyright-footer">Copyright © 2001-2020 Boost.Test contributors<p> 125 Distributed under the Boost Software License, Version 1.0. (See accompanying 126 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>) 127 </p> 128</div></td> 129</tr></table> 130<hr> 131<div class="spirit-nav"> 132<a accesskey="p" href="entry_point.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../single_header_customizations.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="../static_lib_customizations.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a> 133</div> 134</body> 135</html> 136