mirror of
https://github.com/boostorg/test.git
synced 2026-02-02 21:22:10 +00:00
129 lines
6.5 KiB
HTML
Executable File
129 lines
6.5 KiB
HTML
Executable File
<html>
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
|
|
<title>Global fixture</title>
|
|
<link rel="stylesheet" href="../../../../style/style.css" type="text/css">
|
|
<meta name="generator" content="DocBook XSL Stylesheets V1.76.1">
|
|
<link rel="home" href="../../../index.html" title="Boost Test Library">
|
|
<link rel="up" href="../fixture.html" title="Fixtures … or let me repeat myself">
|
|
<link rel="prev" href="test-suite-shared.html" title="Test suite level fixture">
|
|
<link rel="next" href="../initialization.html" title="Test module initialization … or ready, set …">
|
|
<script language="JavaScript1.2" src="../../../../js/boost-test.js"></script>
|
|
</head>
|
|
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
|
|
<table width="100%"><tr>
|
|
<td width="10%"><a href="../../../index.html"><img alt="Home" width="229" height="61" border="0" src="../../../../../../../libs/test/doc/img/boost.test.logo.png"></a></td>
|
|
<td valign="middle" align="left"> > <a href="../../../utf.html">The Unit Test Framework</a><a href="../../../execution-monitor.html">
|
|
>
|
|
</a><a href="../../user-guide.html">User's guide</a><a href="../../usage-recommendations.html">
|
|
>
|
|
</a><a href="../fixture.html">Fixtures</a><a href="../initialization.html">
|
|
>
|
|
</a><b>Global fixture</b>
|
|
</td>
|
|
<td><div class="spirit-nav">
|
|
<a href="test-suite-shared.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a href="../initialization.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a>
|
|
</div></td>
|
|
</tr></table>
|
|
<hr>
|
|
<div class="section">
|
|
<div class="titlepage"><div><div><h5 class="title">
|
|
<a name="utf.user-guide.fixture.global"></a>Global fixture</h5></div></div></div>
|
|
<p class="first-line-indented">
|
|
Any global initialization that needs to be performed every time testing begins or a global cleanup that is to be
|
|
performed once testing is finished is called a global fixture. The <acronym class="acronym">UTF</acronym> global fixture design is based on a
|
|
generic test fixture model and is supported by the utility class boost::unit_test::global_fixture. The global
|
|
fixture design allows any number of global fixtures to be defined in any test file that constitutes a test module.
|
|
Though some initialization can be implemented in the test module initialization function, there are several
|
|
reasons to prefer the global fixture approach:
|
|
</p>
|
|
<div class="itemizedlist"><ul type="disc">
|
|
<li class="listitem">There is no place for cleanup/teardown operations in the initialization function.</li>
|
|
<li class="listitem">
|
|
Unlike the initialization function, the global fixture setup method invocation is guarded by the execution
|
|
monitor. That means that all uncaught errors that occur during initialization are properly reported.
|
|
</li>
|
|
<li class="listitem">
|
|
Any number of different global fixtures can be defined, which allows you to split initialization code by
|
|
category.
|
|
</li>
|
|
<li class="listitem">
|
|
The fixture allows you to place matching setup/teardown code in close vicinity in your test module code.
|
|
</li>
|
|
<li class="listitem">
|
|
If the whole test tree is constructed automatically the initialization function is empty and auto-generated by
|
|
the <acronym class="acronym">UTF</acronym>. To introduce the initialization function can be more work than the use of a global fixture facility,
|
|
while global fixture is more to the point.
|
|
</li>
|
|
<li class="listitem">
|
|
Since all fixtures follow the same generic model you can easily switch from local per test case fixtures to
|
|
the global one.
|
|
</li>
|
|
<li class="listitem">
|
|
If you are using the interactive test runner (non-supplied yet) global test fixtures are applied to every run,
|
|
while an initialization function is executed only once during a test module startup (just make sure that
|
|
it's what you really want).
|
|
</li>
|
|
</ul></div>
|
|
<p class="first-line-indented">
|
|
To define a global test module fixture you need to implement a class that matched generic fixture model and
|
|
passed it as an argument to the macro BOOST_GLOBAL_FIXTURE.
|
|
</p>
|
|
<pre class="inline-synopsis">
|
|
<a name="BOOST_GLOBAL_FIXTURE"></a>BOOST_GLOBAL_FIXTURE(<span class="emphasis"><em>fixure_name</em></span>)</pre>
|
|
<p class="first-line-indented">
|
|
The statement, that performs global fixture definition, has to reside at a test file scope.
|
|
</p>
|
|
<div class="example">
|
|
<a name="utf.user-guide.fixture.global.example20"></a><p class="title"><b>Example 54. Global fixture</b></p>
|
|
<div class="example-contents">
|
|
<pre class="programlisting">#define BOOST_TEST_MODULE example
|
|
#include <boost/test/included/unit_test.hpp>
|
|
#include <iostream>
|
|
|
|
//____________________________________________________________________________//
|
|
|
|
struct MyConfig {
|
|
MyConfig() { std::cout << "global setup\n"; }
|
|
~MyConfig() { std::cout << "global teardown\n"; }
|
|
};
|
|
|
|
//____________________________________________________________________________//
|
|
|
|
BOOST_GLOBAL_FIXTURE( MyConfig );
|
|
|
|
BOOST_AUTO_TEST_CASE( test_case )
|
|
{
|
|
BOOST_CHECK( true );
|
|
}
|
|
|
|
//____________________________________________________________________________//
|
|
|
|
</pre>
|
|
<table border="0" summary="Simple list" class="simplelist"><tr>
|
|
<td><code class="literal"><a href="../../../../src/examples/example20.cpp" target="_top">Source code</a></code></td>
|
|
<td> | </td>
|
|
<td><code class="literal"><a href="#" target="_top" id="id548961" onclick="toggle_element( 'example20-output', 'id548961', 'Show output', 'Hide output' ); return false;">Show output</a></code></td>
|
|
</tr></table>
|
|
<pre class="example-output" id="example20-output">> example
|
|
global setup
|
|
Running 1 test case...
|
|
global teardown
|
|
|
|
*** No errors detected
|
|
</pre>
|
|
</div>
|
|
</div>
|
|
<br class="example-break">
|
|
</div>
|
|
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
|
|
<td align="left"></td>
|
|
<td align="right"><div class="copyright-footer">Copyright © 2001-2012 Gennadiy Rozental</div></td>
|
|
</tr></table>
|
|
<hr>
|
|
<div class="spirit-nav">
|
|
<a accesskey="p" href="test-suite-shared.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../fixture.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="../initialization.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a>
|
|
</div>
|
|
</body>
|
|
</html>
|