mirror of
https://github.com/boostorg/test.git
synced 2026-02-14 13:12:10 +00:00
127 lines
6.4 KiB
HTML
Executable File
127 lines
6.4 KiB
HTML
Executable File
<html>
|
||
<head>
|
||
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
||
<title>Global fixture</title>
|
||
<link rel="stylesheet" href="../../../../style/style.css" type="text/css">
|
||
<meta name="generator" content="DocBook XSL Stylesheets V1.74.0">
|
||
<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="../test-output.html" title="Test Output or let's see what you got for your money">
|
||
<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/docbook/img/boost.test.logo.png"></a></td>
|
||
<td valign="middle" align="left"> > <a href="../../../utf.html">The Unit Test Framework</a> > <a href="../../user-guide.html">User's guide</a><a href="../../testing-tools.html">
|
||
>
|
||
</a><a href="../fixture.html">Fixtures</a><a href="../test-output.html">
|
||
>
|
||
</a><b>Global fixture</b>
|
||
</td>
|
||
<td><div class="spirit-nav">
|
||
<a href="test-suite-shared.html"><img src="../../../../../../../doc/html/images/prev.png" alt="Prev"></a><a href="../test-output.html"><img src="../../../../../../../doc/html/images/next.png" alt="Next"></a>
|
||
</div></td>
|
||
</tr></table>
|
||
<hr>
|
||
<div class="section" lang="en">
|
||
<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 xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="itemizedlist"><ul type="disc">
|
||
<li>There is no place for cleanup/teardown operations in the initialization function.</li>
|
||
<li>
|
||
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>
|
||
Any number of different global fixtures can be defined, which allows you to split initialization code by
|
||
category.
|
||
</li>
|
||
<li>
|
||
The fixture allows you to place matching setup/teardown code in close vicinity in your test module code.
|
||
</li>
|
||
<li>
|
||
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>
|
||
Since all fixtures follow the same generic model you can easily switch from local per test case fixtures to
|
||
the global one.
|
||
</li>
|
||
<li>
|
||
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 25. 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 class="simplelist" border="0" summary="Simple list"><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="id653218" onclick="toggle_element( 'example20-output', 'id653218', '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-2007 Gennadiy Rozental</div></td>
|
||
</tr></table>
|
||
<hr>
|
||
<div class="spirit-nav">
|
||
<a accesskey="p" href="test-suite-shared.html"><img src="../../../../../../../doc/html/images/prev.png" alt="Prev"></a><a accesskey="u" href="../fixture.html"><img src="../../../../../../../doc/html/images/up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../../../../../doc/html/images/home.png" alt="Home"></a><a accesskey="n" href="../test-output.html"><img src="../../../../../../../doc/html/images/next.png" alt="Next"></a>
|
||
</div>
|
||
</body>
|
||
</html>
|