<HTML> <HEAD> <TITLE>The free function based test case</TITLE> <LINK rel="stylesheet" type="text/css" href="../../../../style/btl.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="header"> <A href="../../../../index.html">Boost.Test</A> > <A href="../../../index.html">Components</A> > <A href="../../index.html">The Unit Test Framework</A> > <A href="../index.html">Components</A> > <A href="index.html">The Test Case</A> > <SPAN class="current_article">The free function based test case</SPAN> </DIV> <DIV class="body"> <IMG src='../../../../btl1.gif' width='252' height='43' alt="Boost Test logo"> <H1 class="subtitle">The free function based test case</H1> <H5>Definition</H5> <P class="first-line-indented"> defined in <A href="../../../../../../../boost/test/unit_test_suite.hpp"> unit_test_suite.hpp</A> <H5>Synopsis</H5> <PRE class="code"><SPAN class="reserv-word">class</SPAN> <SPAN class="new-term">function_test_case</SPAN> : <SPAN class="reserv-word">public</SPAN> test_case { ... <SPAN class="comment">// Implementation</SPAN> };</PRE> <H5>Description</H5> <P class="first-line-indented">Simplest and most widely used form of test cases. Instances of the class function_test_case are created by the framework for the user supplied pointer to free function with the following specification: void (*fct)(). Note void return type. To report a testing results one should use the Test Tools instead of returning result code. <H5>Construction</H5> <P class="first-line-indented">To create a test case based on your test function use the following macro:</P> <P><SPAN class="new-term">BOOST_TEST_CASE</SPAN>( free_function_address ).</P> <P class="first-line-indented">BOOST_TEST_CASE creates a new instance of the class function_test_case and returns a pointer to the base class test_case. In most cases you will use it as an argument to the method <A href="../test_suite/index.html">test_suite</A>::add(...). The only parameter to above macro is the pointer of free function - body of the test case.</P> <H5>Examples</H5> <PRE class="code" id="ff_ex_1"><SPAN class="cpp-type">void</SPAN> test_feature1() { ... } ... ts->add( BOOST_TEST_CASE( &test_feature1 ) );</PRE> _________________________________________________ <PRE class="code" id="ff_ex_2">#<SPAN class="reserv-word">include</SPAN> <boost/test/unit_test.hpp> <SPAN class="reserv-word">using</SPAN> boost::unit_test_framework::test_suite; <SPAN class="comment">// most frequently you implement test cases as a free functions</SPAN> <SPAN class="cpp-type">void</SPAN> free_test_function() { BOOST_CHECK( <SPAN class="literal">2</SPAN> == <SPAN class="literal">1</SPAN> ); } test_suite* init_unit_test_suite( <SPAN class="cpp-type">int</SPAN> argc, <SPAN class="cpp-type">char</SPAN>* argv[] ) { test_suite* test= BOOST_TEST_SUITE( <SPAN class="literal">"Example"</SPAN> ); test->add( BOOST_TEST_CASE( &free_test_function ) <SPAN class="reserv-word">return</SPAN> test; }</PRE> </DIV> <DIV class="footer"> <DIV class="footer-body"> <P> © <A name="Copyright">Copyright</A> <A href='mailto:rogeeff@fusemail.com'>Gennadiy Rozental</A> 2001-2004. <BR> Use, modification, and distribution are subject to the Boost Software License, Version 1.0. (See accompanying file <A href="../../../../../../../LICENSE_1_0.txt">LICENSE_1_0.txt</A> or copy at <A href="http://www.boost.org/LICENSE_1_0.txt">www.boost.org/LICENSE_1_0.txt</A>)</P> <P>Revised: <!-- #BeginDate format:Sw1 -->6 January, 2004<!-- #EndDate --> </P> </DIV> </DIV> </BODY> </HTML>