Sophie

Sophie

distrib > Mandriva > 2010.0 > i586 > media > contrib-release > by-pkgid > 27592b0ec0224def9a529ad6725d2ac5 > files > 46

libcunit-devel-2.1.0-8mdv2010.0.i586.rpm

<HTML>
<HEAD>
  <TITLE>CUnit - Managing Tests & Suites</TITLE>
  <LINK REL=StyleSheet HREF="CUnit_doc.css" TYPE="text/css" TITLE="CUnit Basic Style" />
</HEAD>
<BODY>
<DIV CLASS="NAVHEADER" >
<TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0">
  <TR>
    <TH COLSPAN="3" ALIGN="center"><H3>CUnit Progammers Guide</H3></TH>
  </TR>
  <TR>
    <TD WIDTH="10%" ALIGN="left" VALIGN="bottom"><A HREF="test_registry.html" ACCESSKEY="P" >Prev</A></TD>
    <TD WIDTH="80%" ALIGN="center" VALIGN="bottom"><A HREF="index.html" ACCESSKEY="H" >Home</A></TD>
    <TD WIDTH="10%" ALIGN="right" VALIGN="bottom"><A HREF="running_tests.html" ACCESSKEY="N" >Next</A></TD>
  </TR>
</TABLE>
<HR ALIGN="LEFT" WIDTH="100%">

<H2>4. Managing Tests & Suites</H2>

In order for a test to be run by CUnit, it must be added to a test
collection (suite) which is registered with the 
<A HREF="test_registry.html">test registry</A>.

<H3 ID="synopsis">4.1. Synopsis</H3>
#include &lt;<A HREF="headers/TestDB.h">CUnit/TestDB.h</A>&gt;
(included automatically by &lt;<A HREF="headers/CUnit.h">CUnit/CUnit.h</A>&gt;)
<P />
<PRE>
  typedef struct <B CLASS="cite">CU_Suite</B>
  typedef CU_Suite*  <B CLASS="cite">CU_pSuite</B>

  typedef struct <B CLASS="cite">CU_Test</B>
  typedef CU_Test*  <B CLASS="cite">CU_pTest</B>

  typedef void (*<B CLASS="cite">CU_TestFunc</B>)(void)
  typedef int  (*<B CLASS="cite">CU_InitializeFunc</B>)(void)
  typedef int  (*<B CLASS="cite">CU_CleanupFunc</B>)(void)

  CU_pSuite <A HREF="#addsuite">CU_add_suite</A>(const char* strName,
                         CU_InitializeFunc pInit,
                         CU_CleanupFunc pClean);

  CU_pTest  <A HREF="#addtest">CU_add_test</A>(CU_pSuite pSuite,
                        const char* strName,
                        CU_TestFunc pTestFunc);

  typedef struct <B CLASS="cite">CU_TestInfo</B>
  typedef struct <B CLASS="cite">CU_SuiteInfo</B>

  CU_ErrorCode <A HREF="#regsuites">CU_register_suites</A>(CU_SuiteInfo suite_info[]);
  CU_ErrorCode <A HREF="#regsuites">CU_register_nsuites</A>(int suite_count, ...);
</PRE>

<H3 ID="addsuite">4.2. Adding Suites to the Registry</H3>
<P CLASS="indent2"><CITE>CU_pSuite <B>CU_add_suite</B>(const char* strName, CU_InitializeFunc pInit, CU_CleanupFunc pClean)</CITE></P>
<P CLASS="indent5">Creates a new test collection (suite) having
the specified name, initialization function, and cleanup function.
The new suite is registered with (and owned by) the
<A HREF="test_registry.html">test registry</A>, so the registry must be
<A HREF="test_registry.html#init">initialized</A> before adding any suites.
The current implementation does not support the creation of
suites independent of the test registry.
<BR /><BR />
The suite's name must be unique among all suites in the registry.  The
initialization and cleanup functions are optional, and are passed as 
pointers to functions to be called before and after running the tests 
contained in the suite.  This allows the suite to set up and tear down 
temporary fixtures to support running the tests.  These functions take 
no arguments and should return zero if they are completed successfully 
(non-zero otherwise).  If a suite does not require one or both of these 
functions, pass <CODE>NULL</CODE> to <CITE>CU_add_suite()</CITE>.
<BR /><BR />
A pointer to the new suite is returned, which is needed for adding
tests to the suite.  If an error occurs, <CODE>NULL</CODE> is returned and the
framework <A HREF="error_handling.html">error code</A> is set to one
of the following:
</P>
<P CLASS="indent10">
<TABLE CELLPADDING=1>
  <TR>
    <TD><CITE>CUE_SUCCESS</CITE></TD>
    <TD>suite creation was successful.</TD>
  </TR>
  <TR>
    <TD><CITE>CUE_NOREGISTRY</CITE></TD>
    <TD>the registry has not been initialized.</TD>
  </TR>
  <TR>
    <TD><CITE>CUE_NO_SUITENAME</CITE></TD>
    <TD>strName was <CODE>NULL</CODE>.</TD>
  </TR>
  <TR>
    <TD><CITE>CUE_DUP_SUITE</CITE></TD>
    <TD>the suite's name was not unique.</TD>
  </TR>
  <TR>
    <TD><CITE>CUE_NOMEMORY</CITE></TD>
    <TD>memory allocation failed.</TD>
  </TR>
</TABLE>
</P>

<H3 ID="addtest">4.3. Adding Tests to Suites</H3>
<P CLASS="indent2"><CITE>CU_pTest <B>CU_add_test</B>(CU_pSuite pSuite, const char* strName, CU_TestFunc pTestFunc)</CITE></P>
<P CLASS="indent5">Creates a new test having the specified name and
test function, and registers it with the specified suite.  The suite
must already have been created using <A HREF="#addsuite">CU_add_suite()</A>.
The current implementation does not support the creation of tests
independent of a registered suite.
<BR /><BR />
The test's name must be unique among all tests added to a single suite.
The test function cannot be <CODE>NULL</CODE>, and points to a function
to be called when the test is run. Test functions have neither arguments
nor return values.
<BR /><BR />
A pointer to the new test is returned.  If an error occurs during
creation of the test, <CODE>NULL</CODE> is returned and the framework
<A HREF="error_handling.html">error code</A> is set to one of the
following:
</P>
<P CLASS="indent10">
<TABLE CELLPADDING=1>
  <TR>
    <TD><CITE>CUE_SUCCESS</CITE></TD>
    <TD>suite creation was successful.</TD>
  </TR>
  <TR>
    <TD><CITE>CUE_NOREGISTRY</CITE></TD>
    <TD>the registry has not been initialized.</TD>
  </TR>
  <TR>
    <TD><CITE>CUE_NOSUITE</CITE></TD>
    <TD>the specified suite was <CODE>NULL</CODE> or invalid.</TD>
  </TR>
  <TR>
    <TD><CITE>CUE_NO_TESTNAME</CITE></TD>
    <TD>strName was <CODE>NULL</CODE>.</TD>
  </TR>
  <TR>
    <TD><CITE>CUE_NO_TEST</CITE></TD>
    <TD>pTestFunc was <CODE>NULL</CODE> or invalid.</TD>
  </TR>
  <TR>
    <TD><CITE>CUE_DUP_TEST</CITE></TD>
    <TD>the test's name was not unique.</TD>
  </TR>
  <TR>
    <TD><CITE>CUE_NOMEMORY</CITE></TD>
    <TD>memory allocation failed.</TD>
  </TR>
</TABLE>
</P>

<H3 ID="shortcuts">4.4. Shortcut Methods for Managing Tests</H3>
<P CLASS="indent2"><CITE>#define <B>CU_ADD_TEST</B>(suite, test) (CU_add_test(suite, #test, (CU_TestFunc)test))</CITE></P>
<P CLASS="indent5">This macro automatically generates a unique test name
based on the test function name, and adds it to the specified suite.  The return 
value should be checked by the user to verify success.</P>
<P CLASS="indent2" ID="regsuites">
<CITE>CU_ErrorCode <B>CU_register_suites</B>(CU_SuiteInfo suite_info[])</CITE><BR />
<CITE>CU_ErrorCode <B>CU_register_nsuites</B>(int suite_count, ...)</CITE>
</P>
<P CLASS="indent5">For large test structures with many tests and suites,
managing test/suite associations and registration is tedious and error-prone.
CUnit provides a special registration system to help manage suites and tests.
Its primary benefit is to centralize the registration of suites and associated
tests, and to minimize the amount of error checking code the user needs to write.
<BR /><BR />
Test cases are first grouped into arrays of <CITE>CU_TestInfo</CITE> instances
(defined in &lt;<A HREF="headers/TestDB.h">CUnit/TestDB.h</A>&gt;):</P>
<PRE CLASS="indent10">
CU_TestInfo test_array1[] = {
  { "testname1", test_func1 },
  { "testname2", test_func2 },
  { "testname3", test_func3 },
  CU_TEST_INFO_NULL,
};
</PRE>
<P CLASS="indent5">Each array element contains the (unique) name and test
function for a single test case.  The array must end with an element
holding <CODE>NULL</CODE> values, which the macro <CITE>CU_TEST_INFO_NULL</CITE>
conveniently defines.  The test cases included in a single
<CITE>CU_TestInfo</CITE> array form the set of tests that will be registered
with a single test suite.
<BR /><BR />
Suite information is then defined in one or more arrays of 
<CITE>CU_SuiteInfo</CITE> instances (defined in 
&lt;<A HREF="headers/TestDB.h">CUnit/TestDB.h</A>&gt;):</P>
<PRE CLASS="indent10">
CU_SuiteInfo suites[] = {
  { "suitename1", suite1_init-func, suite1_cleanup_func, test_array1 },
  { "suitename2", suite2_init-func, suite2_cleanup_func, test_array2 },
  CU_SUITE_INFO_NULL,
};
</PRE>
<P CLASS="indent5">Each of these array elements contain the (unique) name,
suite initialization function, suite cleanup function, and
<CITE>CU_TestInfo</CITE> array for a single suite.  As usual,
<CODE>NULL</CODE> may be used for the initialization or cleanup
function if the given suite does not need it. The array must end
with an all-<CODE>NULL</CODE> element, for which the macro
<CITE>CU_SUITE_INFO_NULL</CITE> may be used.
<BR /><BR />
All suites defined in a <CITE>CU_SuiteInfo</CITE> array can then be
registered in a single statement:</P>
<P CLASS="indent10">
<CITE>CU_ErrorCode</CITE> error = <CITE>CU_register_suites</CITE>(suites);
</P>
<P CLASS="indent5">If an error occurs during the registration of any suite or
test, an error code is returned.  The error codes are the same as those returned
by normal <A HREF="#addsuite">suite registration</A> and
<A HREF="#addtest">test addition</A> operations.  The function
<CITE>CU_register_nsuites()</CITE> is provided for situations in which
the user wishes to register multiple <CITE>CU_SuiteInfo</CITE> arrays
in a single statement:</P>
<P CLASS="indent10">
<CITE>CU_ErrorCode</CITE> error = <CITE>CU_register_nsuites</CITE>(2, suites1, suites2);
</P>
<P CLASS="indent5">
This function accepts a variable number of <CITE>CU_SuiteInfo</CITE> arrays.
The first argument indicates the actual number ot arrays being passed.
</P>

<H3 ID="deprecated">4.5. Deprecated v1 Data Types & Functions</H3>
The following data types and functions are deprecated as of version 2.
To use these deprecated names, user code must be compiled with
<CITE>USE_DEPRECATED_CUNIT_NAMES</CITE> defined.
<P />
<B>#include&nbsp;&lt;<A HREF="headers/TestDB.h">CUnit/TestDB.h</A>&gt;</B>
(included automatically by <A HREF="headers/CUnit.h">CUnit/CUnit.h</A>&gt;).
<P />
<TABLE CELLPADDING=5 BORDER=2>
  <TR VALIGN="top">
    <TD><B>Deprecated Name</B></TD>
    <TD><B>Equivalent New Name</B></TD>
  </TR>
  <TR VALIGN="top">
    <TD><CODE>TestFunc</CODE></TD>
    <TD><A HREF="#synopsis">CU_TestFunc</A></TD>
  </TR>
  <TR VALIGN="top">
    <TD><CODE>InitializeFunc</CODE></TD>
    <TD><A HREF="#synopsis">CU_InitializeFunc</A></TD>
  </TR>
  <TR VALIGN="top">
    <TD><CODE>CleanupFunc</CODE></TD>
    <TD><A HREF="#synopsis">CU_CleanupFunc</A></TD>
  </TR>
  <TR VALIGN="top">
    <TD><CODE>_TestCase</CODE></TD>
    <TD><A HREF="#synopsis">CU_Test</A></TD>
  </TR>
  <TR VALIGN="top">
    <TD><CODE>PTestCase</CODE></TD>
    <TD><A HREF="#synopsis">CU_pTest</A></TD>
  </TR>
  <TR VALIGN="top">
    <TD><CODE>_TestGroup</CODE></TD>
    <TD><A HREF="#synopsis">CU_Suite</A></TD>
  </TR>
  <TR VALIGN="top">
    <TD><CODE>PTestGroup</CODE></TD>
    <TD><A HREF="#synopsis">CU_pSuite</A></TD>
  </TR>
  <TR VALIGN="top">
    <TD><CODE>add_test_group()</CODE></TD>
    <TD><A HREF="#addsuite">CU_add_suite()</A></TD>
  </TR>
  <TR VALIGN="top">
    <TD><CODE>add_test_case()</CODE></TD>
    <TD><A HREF="#addtest">CU_add_test()</A></TD>
  </TR>
  <TR VALIGN="top">
    <TD><CODE>ADD_TEST_TO_GROUP()</CODE></TD>
    <TD><A HREF="#shortcuts">CU_ADD_TEST()</A></TD>
  </TR>
  <TR VALIGN="top">
    <TD><CODE>test_case_t</CODE></TD>
    <TD><A HREF="#regsuites">CU_TestInfo</A></TD>
  </TR>
  <TR VALIGN="top">
    <TD><CODE>test_group_t</CODE></TD>
    <TD><A HREF="#regsuites">CU_SuiteInfo</A></TD>
  </TR>
  <TR VALIGN="top">
    <TD><CODE>test_suite_t</CODE></TD>
    <TD>no equivalent - use <A HREF="#regsuites">CU_SuiteInfo</A></TD>
  </TR>
  <TR VALIGN="top">
    <TD><CODE>TEST_CASE_NULL</CODE></TD>
    <TD><A HREF="#regsuites">CU_TEST_INFO_NULL</A></TD>
  </TR>
  <TR VALIGN="top">
    <TD><CODE>TEST_GROUP_NULL</CODE></TD>
    <TD><A HREF="#regsuites">CU_SUITE_INFO_NULL</A></TD>
  </TR>
  <TR VALIGN="top">
    <TD><CODE>test_group_register</CODE></TD>
    <TD><A HREF="#regsuites">CU_register_suites()</A></TD>
  </TR>
  <TR VALIGN="top">
    <TD><CODE>test_suite_register</CODE></TD>
    <TD>no equivalent - use <A HREF="#regsuites">CU_register_suites()</A></TD>
  </TR>
</TABLE>

<DIV CLASS="NAVFOOTER">
<HR ALIGN="LEFT" WIDTH="100%">
<TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0">
  <TR>
    <TD WIDTH="33%" ALIGN="left" VALIGN="top"><A HREF="test_registry.html" ACCESSKEY="P">Prev</A></TD>
    <TD WIDTH="34%" ALIGN="center" VALIGN="top"><A HREF="index.html" ACCESSKEY="H" >Home</A></TD>
    <TD WIDTH="33%" ALIGN="right" VALIGN="top"><A HREF="running_tests.html" ACCESSKEY="N" >Next</A></TD>
  </TR>
  <TR>
    <TD WIDTH="33%" ALIGN="left" VALIGN="top">The Test Registry</TD>
    <TD WIDTH="34%" ALIGN="center" VALIGN="top">&nbsp;</TD>
    <TD WIDTH="33%" ALIGN="right" VALIGN="top">Running Tests</TD>
  </TR>
</TABLE>
</DIV>

</BODY>
</HTML>