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">
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>
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>
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>
34 <span class="epigraph">Test everything that could possibly break</span>
35 <span class="epigraph">--XP maxim</span>
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>
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.
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.
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
64 This is how a minimal single-file test program looks like:
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>
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>
76 <div class="calloutlist"><table border="0" summary="Callout list">
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.
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.
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.
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
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).
118 When run, it produces the following output:
120 <pre class="programlisting">Running 1 test case...
121 test_file.cpp(8): error: in "first_test": check i == 2 has failed [1 != 2]
123 *** 1 failure is detected in the test module "My Test"
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-2019 Boost.Test
130 Distributed under the Boost Software License, Version 1.0. (See accompanying
131 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>)
136 <div class="spirit-nav">
137 <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>