blob: 59af236df5b5005d66112b8f6c56d5c97f2efb2a [file] [log] [blame]
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Per test case 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="model.html" title="Generic fixture model">
<link rel="next" href="test-suite-shared.html" title="Test suite level fixture">
<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"> &gt; <a href="../../../utf.html">The Unit Test Framework</a> &gt; <a href="../../user-guide.html">User's guide</a><a href="../../testing-tools.html">
&gt;
</a><a href="../fixture.html">Fixtures</a><a href="../test-output.html">
&gt;
</a><b>Per test case</b>
</td>
<td><div class="spirit-nav">
<a href="model.html"><img src="../../../../../../../doc/html/images/prev.png" alt="Prev"></a><a href="test-suite-shared.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.per-test-case"></a>Per test case fixture</h5></div></div></div>
<p class="first-line-indented">
To automate the task of assigning a fixture for the test case, for test case creation use the macro
BOOST_FIXTURE_TEST_CASE in place of the macro <code class="computeroutput"><a class="link" href="../test-organization/auto-nullary-test-case.html#BOOST_AUTO_TEST_CASE">BOOST_AUTO_TEST_CASE</a></code>:
</p>
<pre class="inline-synopsis">
<a name="BOOST_FIXTURE_TEST_CASE"></a>BOOST_FIXTURE_TEST_CASE(<span class="emphasis"><em>test_case_name</em></span>, <span class="emphasis"><em>fixure_name</em></span>)</pre>
<p class="first-line-indented">
The only difference from the macro <code class="computeroutput"><a class="link" href="../test-organization/auto-nullary-test-case.html#BOOST_AUTO_TEST_CASE">BOOST_AUTO_TEST_CASE</a></code> is the presence of an extra argument
- fixture name. Unlike the pure C++ solution you have direct access to the public and protected members of the
fixture, though you still need to refer to the fixture name in every test case.
</p>
<div class="note"><table border="0" summary="Note">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../../../../doc/html/images/note.png"></td>
<th align="left">Note</th>
</tr>
<tr><td align="left" valign="top"><p>
You can't access private members of fixture, but then why would you make anything private?
</p></td></tr>
</table></div>
<div class="example">
<a name="utf.user-guide.fixture.per-test-case.example18"></a><p class="title"><b>Example 23. Per test case fixture</b></p>
<div class="example-contents">
<p class="first-line-indented">
In this example only test_case1 and test_case2 have fixture F assigned. In the next section you going to see
what can be done if all test cases in a test suite require the same fixture.
</p>
<pre class="programlisting">#define BOOST_TEST_MODULE example
#include &lt;boost/test/included/unit_test.hpp&gt;
struct F {
F() : i( 0 ) { BOOST_TEST_MESSAGE( "setup fixture" ); }
~F() { BOOST_TEST_MESSAGE( "teardown fixture" ); }
int i;
};
BOOST_FIXTURE_TEST_CASE( test_case1, F )
{
BOOST_CHECK( i == 1 );
++i;
}
BOOST_FIXTURE_TEST_CASE( test_case2, F )
{
BOOST_CHECK_EQUAL( i, 1 );
}
BOOST_AUTO_TEST_CASE( test_case3 )
{
BOOST_CHECK( true );
}
</pre>
<table class="simplelist" border="0" summary="Simple list"><tr>
<td><code class="literal"><a href="../../../../src/examples/example18.cpp" target="_top">Source code</a></code></td>
<td> | </td>
<td><code class="literal"><a href="#" target="_top" id="id652742" onclick="toggle_element( 'example18-output', 'id652742', 'Show output', 'Hide output' ); return false;">Show output</a></code></td>
</tr></table>
<pre class="example-output" id="example18-output">&gt; example --log_level=message
Running 3 test cases...
setup fixture
test.cpp(13): error in "test_case1": check i == 1 failed
teardown fixture
setup fixture
test.cpp(19): error in "test_case2": check i == 1 failed [0 != 1]
teardown fixture
*** 2 failures detected in test suite "example"</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="model.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-suite-shared.html"><img src="../../../../../../../doc/html/images/next.png" alt="Next"></a>
</div>
</body>
</html>