1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 4<title>Global fixture</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="../fixtures.html" title="Fixtures"> 9<link rel="prev" href="per_test_suite_fixture.html" title="Test suite entry/exit fixture"> 10<link rel="next" href="../tests_dependencies.html" title="Managing test dependencies"> 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="per_test_suite_fixture.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../fixtures.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="../tests_dependencies.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.tests_organization.fixtures.global"></a><a class="link" href="global.html" title="Global fixture">Global 28 fixture</a> 29</h4></div></div></div> 30<p> 31 Any global initialization that needs to be performed before any test begins, 32 or a cleanup that is to be performed after all tests are finished is called 33 a <span class="emphasis"><em>global fixture</em></span>. A global fixture is equivalent to 34 a <a class="link" href="per_test_suite_fixture.html" title="Test suite entry/exit fixture">test-suite 35 entry/exit</a> fixture (executed once), where in this case the test-suite 36 is the <a class="link" href="../test_tree/master_test_suite.html" title="Master test suite">master 37 test suite</a>. 38 </p> 39<p> 40 The <span class="emphasis"><em>Unit Test Framework</em></span> global fixture design is based 41 on the <a class="link" href="models.html" title="Fixture models">generic 42 test class fixture model</a>. The global fixture design allows any number 43 of global fixtures to be defined in any test file that constitutes a test 44 module. Though some initialization can be implemented in the test module 45 initialization function, there are several reasons to prefer the global 46 fixture approach: 47 </p> 48<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "> 49<li class="listitem"> 50 There is no place for <code class="computeroutput"><span class="identifier">cleanup</span></code>/<code class="computeroutput"><span class="identifier">teardown</span></code> operations in the initialization 51 function. 52 </li> 53<li class="listitem"> 54 Unlike the initialization function, the global fixture construction, 55 <code class="computeroutput"><span class="identifier">setup</span></code> and <code class="computeroutput"><span class="identifier">teardown</span></code> methods invocation are guarded 56 by the execution monitor. That means that all uncaught errors that 57 occur during initialization are properly reported. 58 </li> 59<li class="listitem"> 60 Any number of different global fixtures can be defined, which allows 61 you to split initialization code by category. 62 </li> 63<li class="listitem"> 64 The fixture allows you to place matching <code class="computeroutput"><span class="identifier">setup</span></code>/<code class="computeroutput"><span class="identifier">teardown</span></code> code in close vicinity in 65 your test module code. 66 </li> 67<li class="listitem"> 68 If the whole test tree is constructed automatically, the initialization 69 function is empty and auto-generated by the <span class="emphasis"><em>Unit Test Framework</em></span>. 70 Introducing the initialization function can be more work than using 71 the global fixture facility, while global fixture is more to the point. 72 </li> 73<li class="listitem"> 74 Since all fixtures follow the same generic model you can easily switch 75 from local per test case fixtures to the global one. 76 </li> 77</ul></div> 78<p> 79 To define a global test module fixture you need: 80 </p> 81<div class="orderedlist"><ol class="orderedlist" type="1"> 82<li class="listitem"> 83 to implement a class that matches the <a class="link" href="models.html" title="Fixture models">fixture 84 model</a> 85 </li> 86<li class="listitem"> 87 and to pass the class as an argument to the macro <a class="link" href="../../utf_reference/test_org_reference/test_org_boost_test_global_fixture.html" title="BOOST_TEST_GLOBAL_FIXTURE"><code class="computeroutput"><span class="identifier">BOOST_TEST_GLOBAL_FIXTURE</span></code></a>. 88 </li> 89</ol></div> 90<pre class="programlisting"><span class="identifier">BOOST_TEST_GLOBAL_FIXTURE</span><span class="special">(</span> <span class="identifier">fixture_name</span> <span class="special">);</span> 91</pre> 92<p> 93 The statement, that performs global fixture definition, has to reside at 94 a test file scope. 95 </p> 96<h6> 97<a name="boost_test.tests_organization.fixtures.global.h0"></a> 98 <span class="phrase"><a name="boost_test.tests_organization.fixtures.global.example_descr"></a></span><a class="link" href="global.html#boost_test.tests_organization.fixtures.global.example_descr">Example: 99 Global fixture</a> 100 </h6> 101<div class="informaltable"><table class="table"> 102<colgroup><col></colgroup> 103<thead><tr><th> 104 <p> 105 Code 106 </p> 107 </th></tr></thead> 108<tbody><tr><td> 109<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_MODULE</span> <span class="identifier">fixture_04</span> 110<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> 111 112<span class="keyword">struct</span> <span class="identifier">MyGlobalFixture</span> <span class="special">{</span> 113 <span class="identifier">MyGlobalFixture</span><span class="special">()</span> <span class="special">{</span> 114 <span class="identifier">BOOST_TEST_MESSAGE</span><span class="special">(</span> <span class="string">"ctor fixture i="</span> <span class="special"><<</span> <span class="identifier">i</span> <span class="special">);</span> 115 <span class="special">}</span> 116 <span class="keyword">void</span> <span class="identifier">setup</span><span class="special">()</span> <span class="special">{</span> 117 <span class="identifier">BOOST_TEST_MESSAGE</span><span class="special">(</span> <span class="string">"setup fixture i="</span> <span class="special"><<</span> <span class="identifier">i</span> <span class="special">);</span> 118 <span class="identifier">i</span><span class="special">++;</span> 119 <span class="special">}</span> 120 <span class="keyword">void</span> <span class="identifier">teardown</span><span class="special">()</span> <span class="special">{</span> 121 <span class="identifier">BOOST_TEST_MESSAGE</span><span class="special">(</span> <span class="string">"teardown fixture i="</span> <span class="special"><<</span> <span class="identifier">i</span> <span class="special">);</span> 122 <span class="identifier">i</span> <span class="special">+=</span> <span class="number">2</span><span class="special">;</span> 123 <span class="special">}</span> 124 <span class="special">~</span><span class="identifier">MyGlobalFixture</span><span class="special">()</span> <span class="special">{</span> 125 <span class="identifier">BOOST_TEST_MESSAGE</span><span class="special">(</span> <span class="string">"dtor fixture i="</span> <span class="special"><<</span> <span class="identifier">i</span> <span class="special">);</span> 126 <span class="special">}</span> 127 <span class="keyword">static</span> <span class="keyword">int</span> <span class="identifier">i</span><span class="special">;</span> 128<span class="special">};</span> 129<span class="keyword">int</span> <span class="identifier">MyGlobalFixture</span><span class="special">::</span><span class="identifier">i</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> 130 131<span class="identifier">BOOST_TEST_GLOBAL_FIXTURE</span><span class="special">(</span> <span class="identifier">MyGlobalFixture</span> <span class="special">);</span> 132 133<span class="identifier">BOOST_AUTO_TEST_CASE</span><span class="special">(</span><span class="identifier">test_case1</span><span class="special">)</span> 134<span class="special">{</span> 135 <span class="identifier">BOOST_TEST_MESSAGE</span><span class="special">(</span><span class="string">"running test_case1"</span><span class="special">);</span> 136 <span class="identifier">BOOST_TEST</span><span class="special">(</span><span class="identifier">MyGlobalFixture</span><span class="special">::</span><span class="identifier">i</span> <span class="special">==</span> <span class="number">1</span><span class="special">);</span> 137<span class="special">}</span> 138 139<span class="identifier">BOOST_AUTO_TEST_CASE</span><span class="special">(</span><span class="identifier">test_case2</span><span class="special">)</span> 140<span class="special">{</span> 141 <span class="identifier">BOOST_TEST_MESSAGE</span><span class="special">(</span><span class="string">"running test_case2"</span><span class="special">);</span> 142 <span class="identifier">BOOST_TEST</span><span class="special">(</span><span class="identifier">MyGlobalFixture</span><span class="special">::</span><span class="identifier">i</span> <span class="special">==</span> <span class="number">3</span><span class="special">);</span> 143<span class="special">}</span> 144</pre> 145 </td></tr></tbody> 146</table></div> 147<div class="informaltable"><table class="table"> 148<colgroup><col></colgroup> 149<thead><tr><th> 150 <p> 151 Output 152 </p> 153 </th></tr></thead> 154<tbody><tr><td> 155<pre xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="table-programlisting"><span class="special">></span> <span class="identifier">fixture_04</span> <span class="special">--</span><span class="identifier">log_level</span><span class="special">=</span><span class="identifier">message</span> 156<span class="identifier">Running</span> <span class="number">2</span> <span class="identifier">test</span> <span class="identifier">cases</span><span class="special">...</span> 157<span class="identifier">ctor</span> <span class="identifier">fixture</span> <span class="identifier">i</span><span class="special">=</span><span class="number">0</span> 158<span class="identifier">setup</span> <span class="identifier">fixture</span> <span class="identifier">i</span><span class="special">=</span><span class="number">0</span> 159<span class="identifier">running</span> <span class="identifier">test_case1</span> 160<span class="identifier">running</span> <span class="identifier">test_case2</span> 161<span class="special">./</span><span class="identifier">fixture_04</span><span class="special">.</span><span class="identifier">run</span><span class="special">-</span><span class="identifier">fail</span><span class="special">.</span><span class="identifier">cpp</span><span class="special">:</span><span class="number">42</span><span class="special">:</span> <span class="identifier">error</span><span class="special">:</span> <span class="identifier">in</span> <span class="string">"test_case2"</span><span class="special">:</span> <span class="identifier">check</span> <span class="identifier">MyGlobalFixture</span><span class="special">::</span><span class="identifier">i</span> <span class="special">==</span> <span class="number">3</span> <span class="identifier">has</span> <span class="identifier">failed</span> <span class="special">[</span><span class="number">1</span> <span class="special">!=</span> <span class="number">3</span><span class="special">]</span> 162<span class="identifier">teardown</span> <span class="identifier">fixture</span> <span class="identifier">i</span><span class="special">=</span><span class="number">1</span> 163<span class="identifier">dtor</span> <span class="identifier">fixture</span> <span class="identifier">i</span><span class="special">=</span><span class="number">3</span> 164 165<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">"fixture_04"</span> 166</pre> 167 </td></tr></tbody> 168</table></div> 169</div> 170<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 171<td align="left"></td> 172<td align="right"><div class="copyright-footer">Copyright © 2001-2020 Boost.Test contributors<p> 173 Distributed under the Boost Software License, Version 1.0. (See accompanying 174 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>) 175 </p> 176</div></td> 177</tr></table> 178<hr> 179<div class="spirit-nav"> 180<a accesskey="p" href="per_test_suite_fixture.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../fixtures.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="../tests_dependencies.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a> 181</div> 182</body> 183</html> 184