| 1 | <HTML> |
|---|
| 2 | <HEAD> |
|---|
| 3 | <TITLE>Boost Test Library generic recommendations</TITLE> |
|---|
| 4 | <LINK rel="stylesheet" type="text/css" href="../style/btl.css" media="screen"> |
|---|
| 5 | <LINK rel="stylesheet" type="text/css" href="../style/btl-print.css" media="print"> |
|---|
| 6 | <META http-equiv="Content-Language" content="en-us"> |
|---|
| 7 | <META http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> |
|---|
| 8 | </HEAD> |
|---|
| 9 | <BODY> |
|---|
| 10 | <DIV class="header"> <A href="../index.html">Boost.Test</A> > <A href="recomendations.html"> Usage |
|---|
| 11 | recommendations </A> > <SPAN class="current_article"> Generic</SPAN></DIV> |
|---|
| 12 | <DIV class="body"> <IMG src='../btl1.gif' width='252' height='43' alt="Boost Test logo"> |
|---|
| 13 | <H1 class="subtitle"><SPAN class="first-line-indented">Generic</SPAN> usage |
|---|
| 14 | recommendations</H1> |
|---|
| 15 | <UL> |
|---|
| 16 | <LI><A href="#t2">Prefer the unit test framework to the test execution monitor</A></LI> |
|---|
| 17 | <LI><A href="#t3">Prefer offline compiled libraries to the inline included |
|---|
| 18 | components</A> </LI> |
|---|
| 19 | <LI><A href="#t4">If you use only free function based test cases advance |
|---|
| 20 | to the automatic registration facility</A> </LI> |
|---|
| 21 | <LI><A href="#t1">To find location of first error reported by test tool within |
|---|
| 22 | reused template function, use special hook within framework headers</A></LI> |
|---|
| 23 | <LI><A href="#t5">To test reusable template base component with different |
|---|
| 24 | template parameter use test case template facility</A></LI> |
|---|
| 25 | <LI><A href="#t6">Prefer BOOST_CHECK_EQUAL to BOOST_CHECK</A></LI> |
|---|
| 26 | </UL> |
|---|
| 27 | <H5>Prefer the unit test framework to the test execution monitor<A name="t2"></A> </H5> |
|---|
| 28 | <P class="first-line-indented">In most cases when you start working on unit |
|---|
| 29 | test you have a choice to use either one of the components provided by the |
|---|
| 30 | Boost.Test. Even though in simple cases test cases based solution would be |
|---|
| 31 | couple lines longer in a long term I would opt to use unit test framework. |
|---|
| 32 | At some point you will anyway end up with code like this:</P> |
|---|
| 33 | <PRE class="code"><SPAN class="cpp-type">void</SPAN> test_feature1() |
|---|
| 34 | { |
|---|
| 35 | ... |
|---|
| 36 | |
|---|
| 37 | } |
|---|
| 38 | |
|---|
| 39 | |
|---|
| 40 | <SPAN class="cpp-type">int</SPAN> test_main() |
|---|
| 41 | |
|---|
| 42 | { |
|---|
| 43 | |
|---|
| 44 | test_feature1(); |
|---|
| 45 | test_feature1(); |
|---|
| 46 | ... |
|---|
| 47 | test_featureN(); |
|---|
| 48 | }</PRE> |
|---|
| 49 | <P class="first-line-indented">Why then not switch to test cases? In code like |
|---|
| 50 | above if any exception occurred in test_feature1() rest of test is skipped. |
|---|
| 51 | While with test cases each separate feature test is executed and monitored |
|---|
| 52 | separately and you are getting more from your test. </P> |
|---|
| 53 | <H5>Prefer offline compiled libraries to the inline included components <A name="t3"></A></H5> |
|---|
| 54 | <P class="first-line-indented">If you are just want to write quick simple test |
|---|
| 55 | in environment where you never used Boost.Test before - yes, use included |
|---|
| 56 | components. But if you plan to use Boost.Test on permanent basis, small investment |
|---|
| 57 | of time needed to build (if not build yet), install and change you makefiles/project |
|---|
| 58 | settings will soon return to you in a form of shorter compilation time. Why |
|---|
| 59 | do you need to make your compiler do the same work over and over again? </P> |
|---|
| 60 | <H5>If you use only free function based test cases advance to the automatic |
|---|
| 61 | registration facility<A name="t4"></A></H5> |
|---|
| 62 | <P class="first-line-indented">It's really easy to switch to automatic registration. |
|---|
| 63 | And you don't need to worry about forgotten test case anymore.</P> |
|---|
| 64 | <H5>To find location of first error reported by test tool within reused template |
|---|
| 65 | function, use special hook within framework headers<A name="t1"></A> </H5> |
|---|
| 66 | <P>In some cases you are reusing the same template based code from within one |
|---|
| 67 | test case (actually I recommend better solution in such case- see below). |
|---|
| 68 | Now if an error gets reported by the test tool within that reused code you |
|---|
| 69 | may have difficulty locating were exactly error occurred. To address this |
|---|
| 70 | issue you could either a add BOOST_MESSAGE statements in templated code that |
|---|
| 71 | log current type id od template parameters or you could use special hook |
|---|
| 72 | located in unit_test_result.hpp called first_failed_assertion(). If you set |
|---|
| 73 | a breakpoint right on the line where this function is defined you will be |
|---|
| 74 | able to unroll the stack and see where error actually occurred.</P> |
|---|
| 75 | <H5>To test reusable template base component with different template parameter |
|---|
| 76 | use test case template facility<A name="t5"></A></H5> |
|---|
| 77 | <P class="first-line-indented">If you writing unit test for generic reusable |
|---|
| 78 | component you may have a need to test it against set of different template |
|---|
| 79 | parameter types . Most probably you will end up with a code like this:</P> |
|---|
| 80 | <PRE class="code"><SPAN class="reserv-word">template</SPAN><<SPAN class="reserv-word">typename</SPAN> TestType> |
|---|
| 81 | <SPAN class="cpp-type">void</SPAN> |
|---|
| 82 | specific_type_test( TestType* = <SPAN class="literal">0</SPAN> ) |
|---|
| 83 | { |
|---|
| 84 | MyComponent<TestType> c; |
|---|
| 85 | ... <SPAN class="comment">// here we perform actual testing</SPAN> |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | |
|---|
| 89 | <SPAN class="cpp-type">void</SPAN> my_component_test() |
|---|
| 90 | { |
|---|
| 91 | specific_type_test( (<SPAN class="cpp-type">int</SPAN>*)<SPAN class="literal">0</SPAN> ); |
|---|
| 92 | specific_type_test( (<SPAN class="cpp-type">float</SPAN>*)<SPAN class="literal">0</SPAN> ); |
|---|
| 93 | specific_type_test( (UDT*)<SPAN class="literal">0</SPAN> ); |
|---|
| 94 | ... |
|---|
| 95 | } |
|---|
| 96 | </PRE> |
|---|
| 97 | <P class="first-line-indented">This is namely the situation where you would |
|---|
| 98 | use test case template facility. It's not only simplify this kind of unit |
|---|
| 99 | testing by automating some of the work, in addition every argument type gets |
|---|
| 100 | tested separately under unit test monitor. As a result if one of types produce |
|---|
| 101 | exception or non-fatal error you may still continue and get results from |
|---|
| 102 | testing with other types. </P> |
|---|
| 103 | <H5>Prefer BOOST_CHECK_EQUAL to BOOST_CHECK<A name="t6"></A></H5> |
|---|
| 104 | <P class="first-line-indented">Even though BOOSK_CHECK tool is most generic |
|---|
| 105 | and allows to validate any bool convertible expression, I would recommend |
|---|
| 106 | to use, if possible, more specific tools dedicated to the task. For example |
|---|
| 107 | if you need you validate some variable vs. some expected value use BOOST_CHECK_EQUAL |
|---|
| 108 | instead. The main advantage is that in case of failure you will see the mismatched |
|---|
| 109 | value - the information most useful for error identification. The same applies |
|---|
| 110 | to other tools supplied by the framework. </P> |
|---|
| 111 | </DIV> |
|---|
| 112 | <DIV class="footer"> |
|---|
| 113 | <DIV class="footer-body"> |
|---|
| 114 | <P> © <A name="Copyright">Copyright</A> <A href='mailto:boost-test at emailaccount dot com (please unobscure)'>Gennadiy |
|---|
| 115 | Rozental</A> 2001-2005. <BR> |
|---|
| 116 | Distributed under the Boost Software License, Version 1.0. |
|---|
| 117 | (See accompanying file <A href="../../../../LICENSE_1_0.txt">LICENSE_1_0.txt</A> or |
|---|
| 118 | copy at <A href="http://www.boost.org/LICENSE_1_0.txt">www.boost.org/LICENSE_1_0.txt</A>)</P> |
|---|
| 119 | <P>Revised: |
|---|
| 120 | <!-- #BeginDate format:Sw1 -->26 January, 2004<!-- #EndDate --> |
|---|
| 121 | </P> |
|---|
| 122 | </DIV> |
|---|
| 123 | </DIV> |
|---|
| 124 | </BODY> |
|---|
| 125 | </HTML> |
|---|