1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 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"><</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">></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 69<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> 70<span class="special">{</span> 71 <span class="keyword">int</span> <span class="identifier">i</span> <span class="special">=</span> <span class="number">1</span><span class="special">;</span> 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> <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> 73 <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> 74<span class="special">}</span> 75</pre> 76<div class="calloutlist"><table border="0" summary="Callout list"> 77<tr> 78<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> 79<td valign="top" align="left"><p> 80 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 81 name of our program, which will be used in messages. 82 </p></td> 83</tr> 84<tr> 85<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> 86<td valign="top" align="left"><p> 87 This includes all the <span class="emphasis"><em>Unit Test Framework</em></span> in a "header-only" 88 mode; it even defines function <code class="computeroutput"><span class="identifier">main</span></code>, 89 which will call the subsequently defined test cases. 90 </p></td> 91</tr> 92<tr> 93<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> 94<td valign="top" align="left"><p> 95 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 96 a <span class="emphasis"><em>test case</em></span> named <code class="computeroutput"><span class="identifier">first_test</span></code>, 97 which in turn will run the content of <code class="computeroutput"><span class="identifier">first_test</span></code> 98 inside the controlled testing environment. 99 </p></td> 100</tr> 101<tr> 102<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> 103<td valign="top" align="left"><p> 104 This test checks if <code class="computeroutput"><span class="identifier">i</span></code> is 105 non-zero. 106 </p></td> 107</tr> 108<tr> 109<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> 110<td valign="top" align="left"><p> 111 This test checks if <code class="computeroutput"><span class="identifier">i</span></code> has 112 value <code class="computeroutput"><span class="number">2</span></code> (something more than 113 just evaluating the equality operator). 114 </p></td> 115</tr> 116</table></div> 117<p> 118 When run, it produces the following output: 119 </p> 120<pre class="programlisting">Running 1 test case... 121test_file.cpp(8): error: in "first_test": check i == 2 has failed [1 != 2] 122 123*** 1 failure is detected in the test module "My Test" 124</pre> 125</div> 126<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 127<td align="left"></td> 128<td align="right"><div class="copyright-footer">Copyright © 2001-2020 Boost.Test contributors<p> 129 Distributed under the Boost Software License, Version 1.0. (See accompanying 130 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>) 131 </p> 132</div></td> 133</tr></table> 134<hr> 135<div class="spirit-nav"> 136<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> 137</div> 138</body> 139</html> 140