mirror of
https://github.com/boostorg/test.git
synced 2026-02-18 02:22:09 +00:00
154 lines
9.4 KiB
HTML
154 lines
9.4 KiB
HTML
<html>
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
|
|
<title>Fixtures... or let me repeat myself</title>
|
|
<link rel="stylesheet" href="../../../../../../doc/src/boostbook.css" type="text/css">
|
|
<meta name="generator" content="DocBook XSL Stylesheets V1.78.1">
|
|
<link rel="home" href="../index.html" title="Boost.Test">
|
|
<link rel="up" href="../index.html" title="Boost.Test">
|
|
<link rel="prev" href="the_utf_testing_tools_or_tester_/testing_tool_ref.html" title="UTF testing tools reference">
|
|
<link rel="next" href="fixtures/generic_fixture_model.html" title="Generic fixture model">
|
|
</head>
|
|
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
|
|
<table cellpadding="2" width="100%"><tr>
|
|
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td>
|
|
<td align="center"><a href="../../../../../../index.html">Home</a></td>
|
|
<td align="center"><a href="../../../../../../libs/libraries.htm">Libraries</a></td>
|
|
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
|
|
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
|
|
<td align="center"><a href="../../../../../../more/index.htm">More</a></td>
|
|
</tr></table>
|
|
<hr>
|
|
<div class="spirit-nav">
|
|
<a accesskey="p" href="the_utf_testing_tools_or_tester_/testing_tool_ref.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.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="fixtures/generic_fixture_model.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
|
|
</div>
|
|
<div class="section">
|
|
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
|
|
<a name="boost_test.fixtures"></a><a class="link" href="fixtures.html" title="Fixtures... or let me repeat myself">Fixtures... or let me repeat myself</a>
|
|
</h2></div></div></div>
|
|
<div class="toc"><dl class="toc">
|
|
<dt><span class="section"><a href="fixtures/generic_fixture_model.html">Generic
|
|
fixture model</a></span></dt>
|
|
<dt><span class="section"><a href="fixtures/per_test_case_fixture.html">Per
|
|
test case fixture</a></span></dt>
|
|
<dt><span class="section"><a href="fixtures/test_suite_level_fixture.html">Test
|
|
suite level fixture</a></span></dt>
|
|
<dt><span class="section"><a href="fixtures/global_fixture.html">Global
|
|
fixture</a></span></dt>
|
|
</dl></div>
|
|
<p>
|
|
In general terms a test fixture or test context is the collection of one or
|
|
more of the following items, required to perform the test:
|
|
</p>
|
|
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
|
|
<li class="listitem">
|
|
preconditions
|
|
</li>
|
|
<li class="listitem">
|
|
particular states of tested unites
|
|
</li>
|
|
<li class="listitem">
|
|
necessary cleanup procedures
|
|
</li>
|
|
</ul></div>
|
|
<p>
|
|
Though these tasks are encountered in many if not all test cases, what makes
|
|
a test fixture different is repetition. Where a normal test case implementation
|
|
does all preparatory and cleanup work itself, a test fixture allows this to
|
|
be implemented in a separate reusable unit.
|
|
</p>
|
|
<p>
|
|
With introduction of e<span class="bold"><strong>X</strong></span>treme <span class="bold"><strong>P</strong></span>rogramming
|
|
(XP), the testing style, that require test setup/cleanup repetition, is becoming
|
|
more and more popular. Single XP adopted test modules may contain hundreds
|
|
of single assertion test cases, many requiring very similar test setup/cleanup.
|
|
This is the problem that the test fixture is designed to solve.
|
|
</p>
|
|
<p>
|
|
In practice a test fixture usually is a combination of setup and teardown functions,
|
|
associated with test case. The former serves the purposes of test setup; the
|
|
later is dedicated to the cleanup tasks. Ideally it's preferable that a test
|
|
module author is able to define variables used in fixtures on the stack and
|
|
the same time is able to refer to them directly in test case.
|
|
</p>
|
|
<p>
|
|
It's important to understand that C++ provides a way to implement a straightforward
|
|
test fixture solution that almost satisfies our requirements without any extra
|
|
support from the test framework. This may explain why test fixtures support
|
|
was introduced in the <span class="emphasis"><em>UTF</em></span> somewhat late in its life cycle.
|
|
Here is how simple test module with such a fixture may look like:
|
|
</p>
|
|
<pre class="programlisting"><span class="keyword">struct</span> <span class="identifier">MyFixture</span> <span class="special">{</span>
|
|
<span class="identifier">MyFixture</span><span class="special">()</span> <span class="special">{</span> <span class="identifier">i</span> <span class="special">=</span> <span class="keyword">new</span> <span class="keyword">int</span><span class="special">;</span> <span class="special">*</span><span class="identifier">i</span> <span class="special">=</span> <span class="number">0</span> <span class="special">}</span>
|
|
<span class="special">~</span><span class="identifier">MyFixture</span><span class="special">()</span> <span class="special">{</span> <span class="keyword">delete</span> <span class="identifier">i</span><span class="special">;</span> <span class="special">}</span>
|
|
|
|
<span class="keyword">int</span><span class="special">*</span> <span class="identifier">i</span><span class="special">;</span>
|
|
<span class="special">};</span>
|
|
|
|
<span class="identifier">BOOST_AUTO_TEST_CASE</span><span class="special">(</span> <span class="identifier">test_case1</span> <span class="special">)</span>
|
|
<span class="special">{</span>
|
|
<span class="identifier">MyFixture</span> <span class="identifier">f</span><span class="special">;</span>
|
|
<span class="comment">// do something involving f.i</span>
|
|
<span class="special">}</span>
|
|
|
|
<span class="identifier">BOOST_AUTO_TEST_CASE</span><span class="special">(</span> <span class="identifier">test_case2</span> <span class="special">)</span>
|
|
<span class="special">{</span>
|
|
<span class="identifier">MyFixture</span> <span class="identifier">f</span><span class="special">;</span>
|
|
<span class="comment">// do something involving f.i</span>
|
|
<span class="special">}</span>
|
|
</pre>
|
|
<p>
|
|
This is a generic solution that can be used to implement any kind of shared
|
|
setup or cleanup procedure. Still there are several more or less minor practical
|
|
issues with this pure C++ based fixtures solution:
|
|
</p>
|
|
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
|
|
<li class="listitem">
|
|
We need to add a fixture declaration statement into each test case manually.
|
|
</li>
|
|
<li class="listitem">
|
|
Objects defined in fixture are references with <code class="computeroutput"><span class="special"><</span><span class="identifier">fixture</span><span class="special">-</span><span class="identifier">instance</span><span class="special">-</span><span class="identifier">name</span><span class="special">></span></code>
|
|
prefix.
|
|
</li>
|
|
<li class="listitem">
|
|
There is no place to execute a <span class="emphasis"><em>global</em></span> fixture, which
|
|
performs <span class="emphasis"><em>global</em></span> setup/cleanup procedures before and
|
|
after testing.
|
|
</li>
|
|
</ul></div>
|
|
<p>
|
|
While there is little the <span class="emphasis"><em>UTF</em></span> can do to address these
|
|
issues for manually registered test units, it's possible to resolve them for
|
|
test units that are automatically registered. To do this the <span class="emphasis"><em>UTF</em></span>
|
|
defines a <a class="link" href="fixtures/generic_fixture_model.html#fixtures_generic">generic fixture model</a> - fixed
|
|
interfaces that both setup and teardown fixture functions should comply to.
|
|
Based on the generic fixture model, the <span class="emphasis"><em>UTF</em></span> presents solution
|
|
for the following tasks:
|
|
</p>
|
|
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
|
|
<li class="listitem">
|
|
per test case fixture automation
|
|
</li>
|
|
<li class="listitem">
|
|
shared test suite level fixture
|
|
</li>
|
|
<li class="listitem">
|
|
<span class="emphasis"><em>global</em></span> fixture support
|
|
</li>
|
|
</ul></div>
|
|
</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-2013 Gennadiy Rozental<p>
|
|
Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
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>)
|
|
</p>
|
|
</div></td>
|
|
</tr></table>
|
|
<hr>
|
|
<div class="spirit-nav">
|
|
<a accesskey="p" href="the_utf_testing_tools_or_tester_/testing_tool_ref.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.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="fixtures/generic_fixture_model.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
|
|
</div>
|
|
</body>
|
|
</html>
|