fixtures.html 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
  4. <title>Fixtures</title>
  5. <link rel="stylesheet" href="../../boostbook.css" type="text/css">
  6. <meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
  7. <link rel="home" href="../../index.html" title="Boost.Test">
  8. <link rel="up" href="../tests_organization.html" title="Declaring and organizing tests">
  9. <link rel="prev" href="decorators/explicit_decorator_declaration.html" title="Explicit decorator declaration">
  10. <link rel="next" href="fixtures/models.html" title="Fixture models">
  11. </head>
  12. <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
  13. <table cellpadding="2" width="100%"><tr>
  14. <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td>
  15. <td align="center"><a href="../../../../../../index.html">Home</a></td>
  16. <td align="center"><a href="../../../../../../libs/libraries.htm">Libraries</a></td>
  17. <td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
  18. <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
  19. <td align="center"><a href="../../../../../../more/index.htm">More</a></td>
  20. </tr></table>
  21. <hr>
  22. <div class="spirit-nav">
  23. <a accesskey="p" href="decorators/explicit_decorator_declaration.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../tests_organization.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/models.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
  24. </div>
  25. <div class="section">
  26. <div class="titlepage"><div><div><h3 class="title">
  27. <a name="boost_test.tests_organization.fixtures"></a><a class="link" href="fixtures.html" title="Fixtures">Fixtures</a>
  28. </h3></div></div></div>
  29. <div class="toc"><dl class="toc">
  30. <dt><span class="section"><a href="fixtures/models.html">Fixture
  31. models</a></span></dt>
  32. <dt><span class="section"><a href="fixtures/case.html">Test case
  33. fixture</a></span></dt>
  34. <dt><span class="section"><a href="fixtures/per_test_suite_fixture.html">Test
  35. suite entry/exit fixture</a></span></dt>
  36. <dt><span class="section"><a href="fixtures/global.html">Global
  37. fixture</a></span></dt>
  38. </dl></div>
  39. <p>
  40. In general terms a test fixture or test context is the collection of one
  41. or more of the following items, required to perform the test:
  42. </p>
  43. <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
  44. <li class="listitem">
  45. preconditions
  46. </li>
  47. <li class="listitem">
  48. particular states of tested units
  49. </li>
  50. <li class="listitem">
  51. necessary cleanup procedures
  52. </li>
  53. </ul></div>
  54. <p>
  55. Though these tasks are encountered in many if not all test cases, what makes
  56. a test fixture different is repetition. Where a normal test case implementation
  57. does all preparatory and cleanup work itself, a test fixture allows it to
  58. be implemented in a separate reusable unit.
  59. </p>
  60. <p>
  61. With introduction of e<span class="bold"><strong>X</strong></span>treme <span class="bold"><strong>P</strong></span>rogramming
  62. (XP), the testing style, that require test setup/cleanup repetition, has
  63. become even more popular. Single XP adopted test modules may contain hundreds
  64. of single assertion test cases, many requiring very similar test setup/cleanup.
  65. This is the problem that the test fixture is designed to solve.
  66. </p>
  67. <p>
  68. In practice a test fixture usually is a combination of <code class="computeroutput"><span class="identifier">setup</span></code>
  69. and <code class="computeroutput"><span class="identifier">teardown</span></code> functions, associated
  70. with test case. The former serves the purposes of test setup. The later is
  71. dedicated to the cleanup tasks. Ideally we'd like for a test module author
  72. to be able to define variables used in fixtures on the stack and, at the
  73. same time, to refer to them directly in a test case.
  74. </p>
  75. <p>
  76. It's important to understand that C++ provides a way to implement a straightforward
  77. test fixture solution that almost satisfies our requirements without any
  78. extra support from the test framework. Here is how simple test module with
  79. such a fixture may look like:
  80. </p>
  81. <pre class="programlisting"><span class="keyword">struct</span> <span class="identifier">MyFixture</span> <span class="special">{</span>
  82. <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>
  83. <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>
  84. <span class="keyword">int</span><span class="special">*</span> <span class="identifier">i</span><span class="special">;</span>
  85. <span class="special">};</span>
  86. <a class="link" href="../utf_reference/test_org_reference/test_org_boost_auto_test_case.html" title="BOOST_AUTO_TEST_CASE"><code class="computeroutput"><span class="identifier">BOOST_AUTO_TEST_CASE</span></code></a><span class="special">(</span> <span class="identifier">test_case1</span> <span class="special">)</span>
  87. <span class="special">{</span>
  88. <span class="identifier">MyFixture</span> <span class="identifier">f</span><span class="special">;</span>
  89. <span class="comment">// do something involving f.i</span>
  90. <span class="special">}</span>
  91. <a class="link" href="../utf_reference/test_org_reference/test_org_boost_auto_test_case.html" title="BOOST_AUTO_TEST_CASE"><code class="computeroutput"><span class="identifier">BOOST_AUTO_TEST_CASE</span></code></a><span class="special">(</span> <span class="identifier">test_case2</span> <span class="special">)</span>
  92. <span class="special">{</span>
  93. <span class="identifier">MyFixture</span> <span class="identifier">f</span><span class="special">;</span>
  94. <span class="comment">// do something involving f.i</span>
  95. <span class="special">}</span>
  96. </pre>
  97. <p>
  98. This is a generic solution that can be used to implement any kind of shared
  99. setup or cleanup procedure. Still there are several more or less minor practical
  100. issues with this pure C++ based fixtures solution:
  101. </p>
  102. <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
  103. <li class="listitem">
  104. We need to add a fixture declaration statement into each test case manually.
  105. </li>
  106. <li class="listitem">
  107. Objects defined in fixture are references with <code class="computeroutput"><span class="special">&lt;</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">&gt;</span></code>
  108. prefix.
  109. </li>
  110. <li class="listitem">
  111. There is no place to execute a <span class="emphasis"><em>global</em></span> fixture, which
  112. performs <span class="emphasis"><em>global</em></span> setup/cleanup procedures before
  113. and after testing.
  114. </li>
  115. </ul></div>
  116. <p>
  117. The <span class="emphasis"><em>Unit Test Framework</em></span> lets you define a fixture according
  118. to <a class="link" href="fixtures/models.html" title="Fixture models">several
  119. generic interfaces</a>, and thus helps you with following tasks:
  120. </p>
  121. <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
  122. <li class="listitem">
  123. define shared setup/teardown procedures for a single or group of test
  124. cases
  125. </li>
  126. <li class="listitem">
  127. define setup/teardown procedures which are performed once per test suite
  128. </li>
  129. <li class="listitem">
  130. define <a class="link" href="fixtures/global.html" title="Global fixture">global
  131. setup/teardown</a> procedures which are performed once per test module
  132. </li>
  133. </ul></div>
  134. </div>
  135. <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
  136. <td align="left"></td>
  137. <td align="right"><div class="copyright-footer">Copyright &#169; 2001-2019 Boost.Test
  138. contributors<p>
  139. Distributed under the Boost Software License, Version 1.0. (See accompanying
  140. 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>)
  141. </p>
  142. </div></td>
  143. </tr></table>
  144. <hr>
  145. <div class="spirit-nav">
  146. <a accesskey="p" href="decorators/explicit_decorator_declaration.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../tests_organization.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/models.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
  147. </div>
  148. </body>
  149. </html>