1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
|
<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 <<A HREF="headers/TestDB.h">CUnit/TestDB.h</A>>
(included automatically by <<A HREF="headers/CUnit.h">CUnit/CUnit.h</A>>)
<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 <<A HREF="headers/TestDB.h">CUnit/TestDB.h</A>>):</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
<<A HREF="headers/TestDB.h">CUnit/TestDB.h</A>>):</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 <<A HREF="headers/TestDB.h">CUnit/TestDB.h</A>></B>
(included automatically by <A HREF="headers/CUnit.h">CUnit/CUnit.h</A>>).
<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"> </TD>
<TD WIDTH="33%" ALIGN="right" VALIGN="top">Running Tests</TD>
</TR>
</TABLE>
</DIV>
</BODY>
</HTML>
|