mirror of
https://github.com/boostorg/test.git
synced 2026-01-29 20:12:09 +00:00
129 lines
8.6 KiB
HTML
129 lines
8.6 KiB
HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
|
|
|
|
<HTML>
|
|
<HEAD>
|
|
<TITLE>Boost Test Library: minimal testing</TITLE>
|
|
<LINK rel="stylesheet" type="text/css" href="style/btl-white.css" media="screen">
|
|
<LINK rel="stylesheet" type="text/css" href="style/btl-print.css" media="print">
|
|
<META http-equiv="Content-Language" content="en-us">
|
|
<META http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
|
</HEAD>
|
|
<BODY>
|
|
<DIV class="centered">
|
|
<TABLE class="body-table" cellspacing="3" >
|
|
<TR>
|
|
<TD id="body"> <A name='TOP'><IMG src='../../../c++boost.gif' width='277' height='86' alt="Boost logo"></A>
|
|
<H1>Boost Test Library: minimal testing</H1>
|
|
<P class="page-toc"> <A href="index.htm">Home</A><BR>
|
|
<A href="#Introduction">Introduction</A><BR>
|
|
<A href="#Benefits">Benefits</A><BR>
|
|
<A href="#Example">Example</A><BR>
|
|
<A href="#Tools">Provided Test Tools </A><BR>
|
|
<A href="#Integration">Integration</A><BR>
|
|
<A href="#Test">Test Program</A><BR>
|
|
<A href="#Design">Design</A></P>
|
|
<H2><A name="Introduction">Introduction</A></H2>
|
|
<P class="1-line-indented">Boost Test minimal testing facility provides
|
|
the functionality provided before by the original version of Boost Test.
|
|
The same as the <A href="test_exec_monitor.htm">Test Execution Monitor</A>
|
|
it causes the test program to run in a monitored environment. In addition
|
|
it defines several simple test tools that behave similarly to ones defined
|
|
in <A href="test_tools.htm">Test Tools</A>. Minimal testing facility
|
|
does not require linking with external components, so could be a component
|
|
of choice for simple and quick testing needs. Unlike original Boost Test
|
|
version macro BOOST_INCLUDE_MAIN is not used. main function is included
|
|
unconditionally. One consequence is that minimal testing could not be
|
|
used for multiunit testing. You will need to use other Boost Test components
|
|
for that purpose.</P>
|
|
<H2><A name="Benefits">Benefits</A></H2>
|
|
<P class="1-line-indented">Simple testing framework that provide
|
|
the monitored environment and minimal set of testing tools without need
|
|
to link with external components.</P>
|
|
<H2><A name="Example">Example</A></H2>
|
|
<PRE class="code">#<SPAN class="reserv-word">include</SPAN> <<A href="../../../boost/test/minimal.hpp">boost/test/minimal.hpp</A>>
|
|
|
|
<SPAN class="cpp-type">int</SPAN> add( <SPAN class="cpp-type">int</SPAN> i, <SPAN class="cpp-type">int</SPAN> j ) { <SPAN class="reserv-word">return</SPAN> i+j; }
|
|
|
|
<SPAN class="cpp-type">int</SPAN> test_main( <SPAN class="cpp-type">int</SPAN>, <SPAN class="cpp-type">char </SPAN>*[] ) <SPAN class="comment">// note the name!</SPAN>
|
|
{
|
|
// six ways to detect and report the same error:
|
|
BOOST_CHECK( add( <SPAN class="literal">2</SPAN>,<SPAN class="literal">2</SPAN> ) == <SPAN class="literal">4</SPAN> ); <SPAN class="comment">// #1 continues on error</SPAN>
|
|
BOOST_REQUIRE( add( <SPAN class="literal">2</SPAN>,<SPAN class="literal">2</SPAN> ) == <SPAN class="literal">4</SPAN> ); <SPAN class="comment">// #2 throws on error</SPAN>
|
|
<SPAN class="reserv-word">if</SPAN>( add( <SPAN class="literal">2</SPAN>,<SPAN class="literal">2</SPAN> ) != <SPAN class="literal">4</SPAN> )
|
|
BOOST_ERROR( <SPAN class="literal">"Ouch..."</SPAN> ); <SPAN class="comment">// #3 continues on error</SPAN>
|
|
<SPAN class="reserv-word">if</SPAN>( add( <SPAN class="literal">2</SPAN>,<SPAN class="literal">2</SPAN> ) != <SPAN class="literal">4</SPAN> )
|
|
BOOST_FAIL( <SPAN class="literal">"Ouch..."</SPAN> ); <SPAN class="comment">// #4 throws on error</SPAN>
|
|
<SPAN class="reserv-word">if</SPAN>( add( <SPAN class="literal">2</SPAN>,<SPAN class="literal">2</SPAN> ) != <SPAN class="literal">4</SPAN> ) throw <SPAN class="literal">"Oops..."</SPAN>; <SPAN class="comment">// #5 throws on error</SPAN>
|
|
|
|
<SPAN class="reserv-word">return</SPAN> add( <SPAN class="literal">2</SPAN>, <SPAN class="literal">2</SPAN> ) == <SPAN class="literal">4</SPAN> ? <SPAN class="literal">0</SPAN> : <SPAN class="literal">1</SPAN>; <SPAN class="comment">// #6 returns error code</SPAN>
|
|
}</PRE>
|
|
<P><B>Approach #1</B> uses the BOOST_CHECK tool, which displays an error
|
|
message on std::cout that includes the expression that failed, the source
|
|
file name, and the source file line number. It also increments an error
|
|
count. At program termination, the error count will be displayed automatically
|
|
by the Minimal testing facility.</P>
|
|
<P><B>Approach #2</B> using the BOOST_REQUIRE tool, is similar to #1,
|
|
except that after displaying the error, an exception is thrown, to be
|
|
caught by the Minimal testing facility. This approach is suitable when
|
|
writing a explicit test program, and the error would be so severe as
|
|
to make further testing impractical. BOOST_REQUIRE differs from the
|
|
C++ Standard Library's assert() macro in that it is always generated,
|
|
and channels error detection into the uniform Minimal testing facility
|
|
reporting procedure.</P>
|
|
<P><B>Approaches #3 and #4</B> are similar to #1 and #2 respectively,
|
|
except that the error detection is coded separately. This is most useful
|
|
when the specific condition being tested is not indicative of the reason
|
|
for failure.</P>
|
|
<P><B>Approach #5</B> throws an exception, which will be caught and reported
|
|
by the Minimal testing facility. This approach is suitable for both
|
|
production and test code, in libraries or not. The error message displayed
|
|
when the exception is caught will be most meaningful if the exception
|
|
is derived from std::exception, or is a char* or std::string.</P>
|
|
<P><B>Approach #6</B> uses a return value to inform the caller of the
|
|
error. This approach is particularly suitable for integrating existing
|
|
test code with the test tools library. Although it works fine with the
|
|
Minimal testing facility libraries, and is very useful for running existing
|
|
code under them, most C++ experts prefer using exceptions for error
|
|
reporting. <SPAN class="ref-to-top"><A href="#TOP">
|
|
<IMG src="imgs/uarrow.gif" alt="reference to the top" width="45" height="40"></A></SPAN></P>
|
|
<H2><A name="Tools">Provided Test Tools</A></H2>
|
|
<P class="1-line-indented">Unlike the <A href="test_exec_monitor.htm">Test
|
|
Execution Monitor</A> that support complete set of test tools implemented
|
|
in a <A href="test_tools.htm">Test Tools</A> component, Minimal testing
|
|
facility supply only following four tools:</P>
|
|
<P class="indented"><A href="test_tools.htm#BOOST_CHECK">BOOST_CHECK(
|
|
predicate )</A><BR>
|
|
<A href="test_tools.htm#BOOST_REQUIRE">BOOST_REQUIRE( predicate )</A><BR>
|
|
<A href="test_tools.htm#BOOST_ERROR">BOOST_ERROR( message )</A><BR>
|
|
<A href="test_tools.htm#BOOST_FAIL">BOOST_FAIL( message )</A></P>
|
|
<P class="1-line-indented">Their behavior is similar to ones defined in
|
|
Test Tools component. Follows the links to see more detailed descriptions.
|
|
Old Boost Test tools names are supported either, but will be deprecated
|
|
in a future.</P>
|
|
<H2><A name="Integration">Integration</A></H2>
|
|
<P class="1-line-indented">The only change (other then include boost/test/minimal.hpp)
|
|
you need to make to integrate your test module with Minimal testing
|
|
facility is the signature of your main function. It should look like
|
|
this:</P>
|
|
<P class="indented">int test_main( int argc, char* argv[] )</P>
|
|
<P class="1-line-indented">After that you will automatically start running
|
|
your tests in monitored environment. Also you can start using test tools
|
|
provided by minimal testing facility and get uniform errors reporting.</P>
|
|
<H2><A name="Test">Test Program</A></H2>
|
|
<P class="indented"><a href="components_testing.htm#minimal_test">minimal_test</a></P>
|
|
<H2><A name="Design">Design</A></H2>
|
|
<SPAN class="ref-to-top"><A href="#TOP">
|
|
<IMG src="imgs/uarrow.gif" alt="reference to the top" width="45" height="40"></A></SPAN>
|
|
<P>The <A href="test_lib_design.htm">Boost Test Library Design</A> document
|
|
describes the relationship between Boost Test Library components.</P>
|
|
<DIV class="footer">
|
|
<P>© <A href='mailto:rogeeff@emailaccount.com'>Gennadiy Rozental</A>
|
|
2001-2002 </P>
|
|
<P>Revised: 17 February, 2003</P>
|
|
</DIV>
|
|
</TD>
|
|
</TR>
|
|
</TABLE>
|
|
</DIV>
|
|
</BODY>
|
|
</HTML> |