intro.html 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
  4. <title>Introduction</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="../index.html" title="Boost.Test">
  9. <link rel="prev" href="../index.html" title="Boost.Test">
  10. <link rel="next" href="intro/design_rationale.html" title="Design rationale">
  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="../index.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="intro/design_rationale.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
  24. </div>
  25. <div class="section">
  26. <div class="titlepage"><div><div><h2 class="title" style="clear: both">
  27. <a name="boost_test.intro"></a><a class="link" href="intro.html" title="Introduction">Introduction</a>
  28. </h2></div></div></div>
  29. <div class="toc"><dl class="toc">
  30. <dt><span class="section"><a href="intro/design_rationale.html">Design rationale</a></span></dt>
  31. <dt><span class="section"><a href="intro/how_to_read.html">How to read this documentation</a></span></dt>
  32. </dl></div>
  33. <p>
  34. <span class="epigraph">Test everything that could possibly break</span>
  35. <span class="epigraph">--XP maxim</span>
  36. </p>
  37. <p>
  38. <span class="epigraph">The acceptance test makes the customer satisfied that
  39. the software provides the business value that makes them willing to pay for
  40. it. The unit test makes the programmer satisfied that the software does what
  41. the programmer thinks it does </span> <span class="epigraph">--XP maxim</span>
  42. </p>
  43. <p>
  44. What is the first thing you need to do when you start working on new library/class/program?
  45. That's right - you need to start with the unit test module (hopefully you all
  46. gave this answer!). Occasionally, you may get away with simple test implemented
  47. using <code class="computeroutput"><span class="identifier">assert</span></code>s, but any professional
  48. developer soon finds this approach lacking. It becomes clear that it's too
  49. time-consuming and tedious for simple, but repetitive unit testing tasks and
  50. it's too inflexible for most non-trivial ones.
  51. </p>
  52. <p>
  53. The Boost.Test library provides both an easy to use and flexible set of interfaces
  54. for writing test programs, organizing tests into simple test cases and test
  55. suites, and controlling their runtime execution. Some of Boost.Test's interfaces
  56. are also useful in production (non-test) environments.
  57. </p>
  58. <h5>
  59. <a name="boost_test.intro.h0"></a>
  60. <span class="phrase"><a name="boost_test.intro.starter_example"></a></span><a class="link" href="intro.html#boost_test.intro.starter_example">Starter
  61. example</a>
  62. </h5>
  63. <p>
  64. This is how a minimal single-file test program looks like:
  65. </p>
  66. <pre class="programlisting"><span class="preprocessor">#define</span> <a class="link" href="utf_reference/link_references/link_boost_test_module_macro.html" title="BOOST_TEST_MODULE"><code class="computeroutput"><span class="identifier">BOOST_TEST_MODULE</span></code></a> <span class="identifier">My</span> <span class="identifier">Test</span> <a class="co" name="boost_test.intro.c0" href="intro.html#boost_test.intro.c1"><img src="../../../../../doc/src/images/callouts/1.png" alt="1" border="0"></a>
  67. <span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">test</span><span class="special">/</span><span class="identifier">included</span><span class="special">/</span><span class="identifier">unit_test</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span> <a class="co" name="boost_test.intro.c2" href="intro.html#boost_test.intro.c3"><img src="../../../../../doc/src/images/callouts/2.png" alt="2" border="0"></a>
  68. <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">first_test</span><span class="special">)</span> <a class="co" name="boost_test.intro.c4" href="intro.html#boost_test.intro.c5"><img src="../../../../../doc/src/images/callouts/3.png" alt="3" border="0"></a>
  69. <span class="special">{</span>
  70. <span class="keyword">int</span> <span class="identifier">i</span> <span class="special">=</span> <span class="number">1</span><span class="special">;</span>
  71. <a class="link" href="utf_reference/testing_tool_ref/assertion_boost_test_universal_macro.html" title="BOOST_TEST"><code class="computeroutput"><span class="identifier">BOOST_TEST</span></code></a><span class="special">(</span><span class="identifier">i</span><span class="special">);</span> <a class="co" name="boost_test.intro.c6" href="intro.html#boost_test.intro.c7"><img src="../../../../../doc/src/images/callouts/4.png" alt="4" border="0"></a>
  72. <a class="link" href="utf_reference/testing_tool_ref/assertion_boost_test_universal_macro.html" title="BOOST_TEST"><code class="computeroutput"><span class="identifier">BOOST_TEST</span></code></a><span class="special">(</span><span class="identifier">i</span> <span class="special">==</span> <span class="number">2</span><span class="special">);</span> <a class="co" name="boost_test.intro.c8" href="intro.html#boost_test.intro.c9"><img src="../../../../../doc/src/images/callouts/5.png" alt="5" border="0"></a>
  73. <span class="special">}</span>
  74. </pre>
  75. <div class="calloutlist"><table border="0" summary="Callout list">
  76. <tr>
  77. <td width="5%" valign="top" align="left"><p><a name="boost_test.intro.c1"></a><a href="#boost_test.intro.c0"><img src="../../../../../doc/src/images/callouts/1.png" alt="1" border="0"></a> </p></td>
  78. <td valign="top" align="left"><p>
  79. Macro <a class="link" href="utf_reference/link_references/link_boost_test_module_macro.html" title="BOOST_TEST_MODULE"><code class="computeroutput"><span class="identifier">BOOST_TEST_MODULE</span></code></a> defines the
  80. name of our program, which will be used in messages.
  81. </p></td>
  82. </tr>
  83. <tr>
  84. <td width="5%" valign="top" align="left"><p><a name="boost_test.intro.c3"></a><a href="#boost_test.intro.c2"><img src="../../../../../doc/src/images/callouts/2.png" alt="2" border="0"></a> </p></td>
  85. <td valign="top" align="left"><p>
  86. This includes all the <span class="emphasis"><em>Unit Test Framework</em></span> in a "header-only"
  87. mode; it even defines function <code class="computeroutput"><span class="identifier">main</span></code>,
  88. which will call the subsequently defined test cases.
  89. </p></td>
  90. </tr>
  91. <tr>
  92. <td width="5%" valign="top" align="left"><p><a name="boost_test.intro.c5"></a><a href="#boost_test.intro.c4"><img src="../../../../../doc/src/images/callouts/3.png" alt="3" border="0"></a> </p></td>
  93. <td valign="top" align="left"><p>
  94. Macro <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> declares
  95. a <span class="emphasis"><em>test case</em></span> named <code class="computeroutput"><span class="identifier">first_test</span></code>,
  96. which in turn will run the content of <code class="computeroutput"><span class="identifier">first_test</span></code>
  97. inside the controlled testing environment.
  98. </p></td>
  99. </tr>
  100. <tr>
  101. <td width="5%" valign="top" align="left"><p><a name="boost_test.intro.c7"></a><a href="#boost_test.intro.c6"><img src="../../../../../doc/src/images/callouts/4.png" alt="4" border="0"></a> </p></td>
  102. <td valign="top" align="left"><p>
  103. This test checks if <code class="computeroutput"><span class="identifier">i</span></code> is
  104. non-zero.
  105. </p></td>
  106. </tr>
  107. <tr>
  108. <td width="5%" valign="top" align="left"><p><a name="boost_test.intro.c9"></a><a href="#boost_test.intro.c8"><img src="../../../../../doc/src/images/callouts/5.png" alt="5" border="0"></a> </p></td>
  109. <td valign="top" align="left"><p>
  110. This test checks if <code class="computeroutput"><span class="identifier">i</span></code> has
  111. value <code class="computeroutput"><span class="number">2</span></code> (something more than
  112. just evaluating the equality operator).
  113. </p></td>
  114. </tr>
  115. </table></div>
  116. <p>
  117. When run, it produces the following output:
  118. </p>
  119. <pre class="programlisting">Running 1 test case...
  120. test_file.cpp(8): error: in "first_test": check i == 2 has failed [1 != 2]
  121. *** 1 failure is detected in the test module "My Test"
  122. </pre>
  123. </div>
  124. <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
  125. <td align="left"></td>
  126. <td align="right"><div class="copyright-footer">Copyright &#169; 2001-2019 Boost.Test
  127. contributors<p>
  128. Distributed under the Boost Software License, Version 1.0. (See accompanying
  129. 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>)
  130. </p>
  131. </div></td>
  132. </tr></table>
  133. <hr>
  134. <div class="spirit-nav">
  135. <a accesskey="p" href="../index.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="intro/design_rationale.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
  136. </div>
  137. </body>
  138. </html>