File: fixture.html

package info (click to toggle)
boost1.42 1.42.0-4
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 277,864 kB
  • ctags: 401,076
  • sloc: cpp: 1,235,659; xml: 74,142; ansic: 41,313; python: 26,756; sh: 11,840; cs: 2,118; makefile: 655; perl: 494; yacc: 456; asm: 353; csh: 6
file content (120 lines) | stat: -rwxr-xr-x 6,581 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Fixtures or let me repeat myself</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="../user-guide.html" title="Unit Test Framework: User's guide">
<link rel="prev" href="test-organization/expected-failures.html" title="Expected failures specification">
<link rel="next" href="fixture/model.html" title="Generic fixture model">
<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><b>Fixtures</b><a href="test-output.html">
      &gt;
      </a>
</td>
<td><div class="spirit-nav">
<a href="test-organization/expected-failures.html"><img src="../../../../../../doc/html/images/prev.png" alt="Prev"></a><a href="fixture/model.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><h4 class="title">
<a name="utf.user-guide.fixture"></a>Fixtures  or let me repeat myself</h4></div></div></div>
<p class="first-line-indented">
  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 xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="itemizedlist"><ul type="disc">
<li>preconditions</li>
<li>particular states of tested unites</li>
<li>necessary cleanup procedures</li>
</ul></div>
<p class="first-line-indented">
  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 class="first-line-indented">
  With introduction of extreme programming (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 class="first-line-indented">
  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 class="first-line-indented">
  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 <acronym class="acronym">UTF</acronym> somewhat late in its life cycle. Here is how simple test module
  with such a fixture may look like:
 </p>
<pre class="programlisting">struct MyFixture {
     MyFixture() { i = new int; *i = 0 }
     ~ MyFixture() { delete i; }

    int* i;
};

BOOST_AUTO_TEST_CASE( test_case1 )
{
    MyFixture f;

    // do something involving f.i
}

BOOST_AUTO_TEST_CASE( test_case2 )
{
    MyFixture f;

    // do something involving f.i
}
</pre>
<p class="first-line-indented">
  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 xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="itemizedlist"><ul type="disc">
<li>
    We need to add a fixture declaration statement into each test case manually.
   </li>
<li>
    Objects defined in fixture are references with "&lt;fixture-instance-name&gt;." prefix.
   </li>
<li>
    There is no place to execute a "global" fixture, which performs "global" setup/cleanup
    procedures before and after testing.
   </li>
</ul></div>
<p class="first-line-indented">
  While there is little the <acronym class="acronym">UTF</acronym> 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 <acronym class="acronym">UTF</acronym> defines a
  <a class="link" href="fixture/model.html" title="Generic fixture model">generic fixture model</a> - fixed interfaces that both setup and
  teardown fixture functions should comply to. Based on the generic fixture model, the <acronym class="acronym">UTF</acronym> presents solution for
  the following tasks:
 </p>
<div xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="itemizedlist"><ul type="disc">
<li><a class="link" href="fixture/per-test-case.html" title="Per test case fixture">per test case fixture automation</a></li>
<li><a class="link" href="fixture/test-suite-shared.html" title="Test suite level fixture">shared test suite level fixture</a></li>
<li><a class="link" href="fixture/global.html" title="Global fixture">"global" fixture support</a></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-2007 Gennadiy Rozental</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="test-organization/expected-failures.html"><img src="../../../../../../doc/html/images/prev.png" alt="Prev"></a><a accesskey="u" href="../user-guide.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="fixture/model.html"><img src="../../../../../../doc/html/images/next.png" alt="Next"></a>
</div>
</body>
</html>