| 1 | <HTML> |
| 2 | <HEAD> |
| 3 | <TITLE>CUnit - Writing CUnit Test Cases</TITLE> |
| 4 | <LINK REL=StyleSheet HREF="CUnit_doc.css" TYPE="text/css" TITLE="CUnit Basic Style" /> |
| 5 | </HEAD> |
| 6 | <BODY> |
| 7 | <DIV CLASS="NAVHEADER" > |
| 8 | <TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0"> |
| 9 | <TR> |
| 10 | <TH COLSPAN="3" ALIGN="center"><H3>CUnit Progammers Guide</H3></TH> |
| 11 | </TR> |
| 12 | <TR> |
| 13 | <TD WIDTH="10%" ALIGN="left" VALIGN="bottom"><A HREF="introduction.html" ACCESSKEY="P" >Prev</A></TD> |
| 14 | <TD WIDTH="80%" ALIGN="center" VALIGN="bottom"><A HREF="index.html" ACCESSKEY="H" >Home</A></TD> |
| 15 | <TD WIDTH="10%" ALIGN="right" VALIGN="bottom"><A HREF="test_registry.html" ACCESSKEY="N" >Next</A></TD> |
| 16 | </TR> |
| 17 | </TABLE> |
| 18 | <HR ALIGN="LEFT" WIDTH="100%"> |
| 19 | |
| 20 | <H2>2. Writing CUnit Test Cases</H2> |
| 21 | |
| 22 | <H3 ID="tests">2.1. Test Functions</H3> |
| 23 | A CUnit "test" is a C function having the signature:<BR /><BR /> |
| 24 | <B><I>void test_func(void)</I></B> |
| 25 | <P /> |
| 26 | There are no restrictions on the content of a test function, except |
| 27 | that it should not modify the CUnit framework (e.g. add suites or tests, |
| 28 | modify the test registry, or initiate a test run). A test function may |
| 29 | call other functions (which also may not modify the framework). |
| 30 | Registering a test will cause its function to be run when the |
| 31 | test is run. |
| 32 | <P /> |
| 33 | An example test function for a routine that returns the maximum of 2 |
| 34 | integers might look like: |
| 35 | <PRE><CODE> |
| 36 | int maxi(int i1, int i2) |
| 37 | { |
| 38 | return (i1 > i2) ? i1 : i2; |
| 39 | } |
| 40 | |
| 41 | void test_maxi(void) |
| 42 | { |
| 43 | CU_ASSERT(maxi(0,2) == 2); |
| 44 | CU_ASSERT(maxi(0,-2) == 0); |
| 45 | CU_ASSERT(maxi(2,2) == 2); |
| 46 | } |
| 47 | </CODE></PRE> |
| 48 | <H3 ID="assertions">2.2. CUnit Assertions</H3> |
| 49 | CUnit provides a set of assertions for testing logical conditions. The |
| 50 | success or failure of these assertions is tracked by the framework, |
| 51 | and can be viewed when a test run is complete. |
| 52 | <P /> |
| 53 | Each assertion tests a single logical condition, and fails if the |
| 54 | condition evaluates to <CODE>CU_FALSE</CODE>. Upon assertion failure, |
| 55 | the test function continues unless the user chooses the 'xxx_FATAL' version |
| 56 | of an assertion. In that case, the test function is aborted and returns |
| 57 | immediately. <B>FATAL versions of assertions should be used with caution!</B> |
| 58 | There is no opportunity for the test function to clean up after itself once a |
| 59 | FATAL assertion fails. The normal <A HREF="managing_tests.html#addsuite">suite |
| 60 | cleanup function</A> is not affected, however, and will be run in either case. |
| 61 | <P /> |
| 62 | There are also special "assertions" for registering a |
| 63 | <A HREF="#pass">pass</A> or <A HREF="#fail">fail</A> with the framework |
| 64 | without performing a logical test. These are useful for testing flow |
| 65 | of control or other conditions not requiring a logical test: |
| 66 | <PRE><CODE> |
| 67 | void test_longjmp(void) |
| 68 | { |
| 69 | jmp_buf buf; |
| 70 | int i; |
| 71 | |
| 72 | i = setjmp(buf); |
| 73 | if (i == 0) { |
| 74 | run_other_func(); |
| 75 | CU_PASS("run_other_func() succeeded."); |
| 76 | } |
| 77 | else |
| 78 | CU_FAIL("run_other_func() issued longjmp."); |
| 79 | } |
| 80 | </CODE></PRE> |
| 81 | <P /> |
| 82 | Other functions called by a registered test function may use the CUnit |
| 83 | assertions freely. These assertions will be counted for the calling |
| 84 | function. They may also use FATAL versions of assertions - failure |
| 85 | will abort the original test function and its entire call chain. |
| 86 | <P /> |
| 87 | The assertions defined by CUnit are: |
| 88 | <P /> |
| 89 | <B>#include <<A HREF="headers/CUnit.h">CUnit/CUnit.h</A>></B> |
| 90 | <P /> |
| 91 | <TABLE CELLPADDING=5 BORDER=2> |
| 92 | <TR VALIGN="top"> |
| 93 | <TD> |
| 94 | <CODE> |
| 95 | <CITE><B>CU_ASSERT</B>(int expression)</CITE><BR /> |
| 96 | <CITE><B>CU_ASSERT_FATAL</B>(int expression)</CITE><BR /> |
| 97 | <CITE><B>CU_TEST</B>(int expression)</CITE><BR /> |
| 98 | <CITE><B>CU_TEST_FATAL</B>(int expression)</CITE> |
| 99 | </CODE> |
| 100 | </TD> |
| 101 | <TD>Assert that <I>expression</I> is <CODE>CU_TRUE</CODE> (non-zero)</TD> |
| 102 | </TR> |
| 103 | <TR VALIGN="top"> |
| 104 | <TD> |
| 105 | <CODE> |
| 106 | <CITE><B>CU_ASSERT_TRUE</B>(value)</CITE><BR /> |
| 107 | <CITE><B>CU_ASSERT_TRUE_FATAL</B>(value)</CITE> |
| 108 | </CODE> |
| 109 | </TD> |
| 110 | <TD>Assert that <I>value</I> is <CODE>CU_TRUE (non-zero)</TD> |
| 111 | </TR> |
| 112 | <TR VALIGN="top"> |
| 113 | <TD> |
| 114 | <CODE> |
| 115 | <CITE><B>CU_ASSERT_FALSE</B>(value)</CITE><BR /> |
| 116 | <CITE><B>CU_ASSERT_FALSE_FATAL</B>(value)</CITE> |
| 117 | </CODE> |
| 118 | </TD> |
| 119 | <TD>Assert that <I>value</I> is <CODE>CU_FALSE</CODE> (zero)</TD> |
| 120 | </TR> |
| 121 | <TR VALIGN="top"> |
| 122 | <TD> |
| 123 | <CODE> |
| 124 | <CITE><B>CU_ASSERT_EQUAL</B>(actual, expected)</CITE><BR /> |
| 125 | <CITE><B>CU_ASSERT_EQUAL_FATAL</B>(actual, expected)</CITE> |
| 126 | </CODE> |
| 127 | </TD> |
| 128 | <TD>Assert that <I>actual</I> = = <I>expected</I></TD> |
| 129 | </TR> |
| 130 | <TR VALIGN="top"> |
| 131 | <TD> |
| 132 | <CODE> |
| 133 | <CITE><B>CU_ASSERT_NOT_EQUAL</B>(actual, expected))</CITE><BR /> |
| 134 | <CITE><B>CU_ASSERT_NOT_EQUAL_FATAL</B>(actual, expected)</CITE> |
| 135 | </CODE> |
| 136 | </TD> |
| 137 | <TD>Assert that <I>actual</I> != <I>expected</I></TD> |
| 138 | </TR> |
| 139 | <TR VALIGN="top"> |
| 140 | <TD> |
| 141 | <CODE> |
| 142 | <CITE><B>CU_ASSERT_PTR_EQUAL</B>(actual, expected)</CITE><BR /> |
| 143 | <CITE><B>CU_ASSERT_PTR_EQUAL_FATAL</B>(actual, expected)</CITE> |
| 144 | </CODE> |
| 145 | </TD> |
| 146 | <TD>Assert that pointers <I>actual</I> = = <I>expected</I></TD> |
| 147 | </TR> |
| 148 | <TR VALIGN="top"> |
| 149 | <TD> |
| 150 | <CODE> |
| 151 | <CITE><B>CU_ASSERT_PTR_NOT_EQUAL</B>(actual, expected)</CITE><BR /> |
| 152 | <CITE><B>CU_ASSERT_PTR_NOT_EQUAL_FATAL</B>(actual, expected)</CITE> |
| 153 | </CODE> |
| 154 | </TD> |
| 155 | <TD>Assert that pointers <I>actual</I> != <I>expected</I></TD> |
| 156 | </TR> |
| 157 | <TR VALIGN="top"> |
| 158 | <TD> |
| 159 | <CODE> |
| 160 | <CITE><B>CU_ASSERT_PTR_NULL</B>(value)</CITE><BR /> |
| 161 | <CITE><B>CU_ASSERT_PTR_NULL_FATAL</B>(value)</CITE> |
| 162 | </CODE> |
| 163 | </TD> |
| 164 | <TD>Assert that pointer <I>value</I> == NULL</TD> |
| 165 | </TR> |
| 166 | <TR VALIGN="top"> |
| 167 | <TD> |
| 168 | <CODE> |
| 169 | <CITE><B>CU_ASSERT_PTR_NOT_NULL</B>(value)</CITE><BR /> |
| 170 | <CITE><B>CU_ASSERT_PTR_NOT_NULL_FATAL</B>(value)</CITE> |
| 171 | </CODE> |
| 172 | </TD> |
| 173 | <TD>Assert that pointer <I>value</I> != NULL</TD> |
| 174 | </TR> |
| 175 | <TR VALIGN="top"> |
| 176 | <TD> |
| 177 | <CODE> |
| 178 | <CITE><B>CU_ASSERT_STRING_EQUAL</B>(actual, expected)</CITE><BR /> |
| 179 | <CITE><B>CU_ASSERT_STRING_EQUAL_FATAL</B>(actual, expected)</CITE> |
| 180 | </CODE> |
| 181 | </TD> |
| 182 | <TD>Assert that strings <I>actual</I> and <I>expected</I> are equivalent</TD> |
| 183 | </TR> |
| 184 | <TR VALIGN="top"> |
| 185 | <TD> |
| 186 | <CODE> |
| 187 | <CITE><B>CU_ASSERT_STRING_NOT_EQUAL</B>(actual, expected)</CITE><BR /> |
| 188 | <CITE><B>CU_ASSERT_STRING_NOT_EQUAL_FATAL</B>(actual, expected)</CITE> |
| 189 | </CODE> |
| 190 | </TD> |
| 191 | <TD>Assert that strings <I>actual</I> and <I>expected</I> differ</TD> |
| 192 | </TR> |
| 193 | <TR VALIGN="top"> |
| 194 | <TD> |
| 195 | <CODE> |
| 196 | <CITE><B>CU_ASSERT_NSTRING_EQUAL</B>(actual, expected, count)</CITE><BR /> |
| 197 | <CITE><B>CU_ASSERT_NSTRING_EQUAL_FATAL</B>(actual, expected, count)</CITE> |
| 198 | </CODE> |
| 199 | </TD> |
| 200 | <TD>Assert that 1st count chars of <I>actual</I> and <I>expected</I> are the same</TD> |
| 201 | </TR> |
| 202 | <TR VALIGN="top"> |
| 203 | <TD> |
| 204 | <CODE> |
| 205 | <CITE><B>CU_ASSERT_NSTRING_NOT_EQUAL</B>(actual, expected, count)</CITE><BR /> |
| 206 | <CITE><B>CU_ASSERT_NSTRING_NOT_EQUAL_FATAL</B>(actual, expected, count)</CITE> |
| 207 | </CODE> |
| 208 | </TD> |
| 209 | <TD>Assert that 1st count chars of <I>actual</I> and <I>expected</I> differ</TD> |
| 210 | </TR> |
| 211 | <TR VALIGN="top"> |
| 212 | <TD> |
| 213 | <CODE> |
| 214 | <CITE><B>CU_ASSERT_DOUBLE_EQUAL</B>(actual, expected, granularity)</CITE><BR /> |
| 215 | <CITE><B>CU_ASSERT_DOUBLE_EQUAL_FATAL</B>(actual, expected, granularity)</CITE> |
| 216 | </CODE> |
| 217 | </TD> |
| 218 | <TD> |
| 219 | Assert that |<I>actual</I> - <I>expected</I>| <= |<I>granularity</I>|<BR /> |
| 220 | <I>Math library must be linked in for this assertion.</I> |
| 221 | </TD> |
| 222 | </TR> |
| 223 | <TR VALIGN="top"> |
| 224 | <TD> |
| 225 | <CODE> |
| 226 | <CITE><B>CU_ASSERT_DOUBLE_NOT_EQUAL</B>(actual, expected, granularity)</CITE><BR /> |
| 227 | <CITE><B>CU_ASSERT_DOUBLE_NOT_EQUAL_FATAL</B>(actual, expected, granularity)</CITE> |
| 228 | </CODE> |
| 229 | </TD> |
| 230 | <TD> |
| 231 | Assert that |<I>actual</I> - <I>expected</I>| > |<I>granularity</I>|<BR /> |
| 232 | <I>Math library must be linked in for this assertion.</I> |
| 233 | </TD> |
| 234 | </TR> |
| 235 | <TR VALIGN="top" ID="pass"> |
| 236 | <TD> |
| 237 | <CODE> |
| 238 | <CITE><B>CU_PASS</B>(message)</CITE> |
| 239 | </CODE> |
| 240 | </TD> |
| 241 | <TD> |
| 242 | Register a passing assertion with the specified message. No logical test is performed. |
| 243 | </TD> |
| 244 | </TR> |
| 245 | <TR VALIGN="top" ID="fail"> |
| 246 | <TD> |
| 247 | <CODE> |
| 248 | <CITE><B>CU_FAIL</B>(message)</CITE><BR /> |
| 249 | <CITE><B>CU_FAIL_FATAL</B>(message)</CITE> |
| 250 | </CODE> |
| 251 | </TD> |
| 252 | <TD> |
| 253 | Register a failed assertion with the specified message. No logical test is performed. |
| 254 | </TD> |
| 255 | </TR> |
| 256 | </TABLE> |
| 257 | <P /> |
| 258 | <H3 ID="deprecated">2.3. Depecated v1 Assertions</H3> |
| 259 | The following assertions are deprecated as of version 2. To use these assertions, |
| 260 | user code must be compiled with <CITE>USE_DEPRECATED_CUNIT_NAMES</CITE> defined. |
| 261 | Note that they behave the same as in version 1 (issue a 'return' statement |
| 262 | upon failure). |
| 263 | <P /> |
| 264 | <B>#include <<A HREF="headers/CUnit.h">CUnit/CUnit.h</A>></B> |
| 265 | <P /> |
| 266 | <TABLE CELLPADDING=5 BORDER=2> |
| 267 | <TR VALIGN="top"> |
| 268 | <TD><B>Deprecated Name</B></TD> |
| 269 | <TD><B>Equivalent New Name</B></TD> |
| 270 | </TR> |
| 271 | <TR VALIGN="top"> |
| 272 | <TD><CODE>ASSERT</CODE></TD> |
| 273 | <TD><CODE>CU_ASSERT_FATAL</CODE></TD> |
| 274 | </TR> |
| 275 | <TR VALIGN="top"> |
| 276 | <TD><CODE>ASSERT_TRUE</CODE></TD> |
| 277 | <TD><CODE>CU_ASSERT_TRUE_FATAL</CODE></TD> |
| 278 | </TR> |
| 279 | <TR VALIGN="top"> |
| 280 | <TD><CODE>ASSERT_FALSE</CODE></TD> |
| 281 | <TD><CODE>CU_ASSERT_FALSE_FATAL</CODE></TD> |
| 282 | </TR> |
| 283 | <TR VALIGN="top"> |
| 284 | <TD><CODE>ASSERT_EQUAL</CODE></TD> |
| 285 | <TD><CODE>CU_ASSERT_EQUAL_FATAL</CODE></TD> |
| 286 | </TR> |
| 287 | <TR VALIGN="top"> |
| 288 | <TD><CODE>ASSERT_NOT_EQUAL</CODE></TD> |
| 289 | <TD><CODE>CU_ASSERT_NOT_EQUAL_FATAL</CODE></TD> |
| 290 | </TR> |
| 291 | <TR VALIGN="top"> |
| 292 | <TD><CODE>ASSERT_PTR_EQUAL</CODE></TD> |
| 293 | <TD><CODE>CU_ASSERT_PTR_EQUAL_FATAL</CODE></TD> |
| 294 | </TR> |
| 295 | <TR VALIGN="top"> |
| 296 | <TD><CODE>ASSERT_PTR_NOT_EQUAL</CODE></TD> |
| 297 | <TD><CODE>CU_ASSERT_PTR_NOT_EQUAL_FATAL</CODE></TD> |
| 298 | </TR> |
| 299 | <TR VALIGN="top"> |
| 300 | <TD><CODE>ASSERT_PTR_NULL</CODE></TD> |
| 301 | <TD><CODE>CU_ASSERT_PTR_NULL_FATAL</CODE></TD> |
| 302 | </TR> |
| 303 | <TR VALIGN="top"> |
| 304 | <TD><CODE>ASSERT_PTR_NOT_NULL</CODE></TD> |
| 305 | <TD><CODE>CU_ASSERT_PTR_NOT_NULL_FATAL</CODE></TD> |
| 306 | </TR> |
| 307 | <TR VALIGN="top"> |
| 308 | <TD><CODE>ASSERT_STRING_EQUAL</CODE></TD> |
| 309 | <TD><CODE>CU_ASSERT_STRING_EQUAL_FATAL</CODE></TD> |
| 310 | </TR> |
| 311 | <TR VALIGN="top"> |
| 312 | <TD><CODE>ASSERT_STRING_NOT_EQUAL</CODE></TD> |
| 313 | <TD><CODE>CU_ASSERT_STRING_NOT_EQUAL_FATAL</CODE></TD> |
| 314 | </TR> |
| 315 | <TR VALIGN="top"> |
| 316 | <TD><CODE>ASSERT_NSTRING_EQUAL</CODE></TD> |
| 317 | <TD><CODE>CU_ASSERT_NSTRING_EQUAL_FATAL</CODE></TD> |
| 318 | </TR> |
| 319 | <TR VALIGN="top"> |
| 320 | <TD><CODE>ASSERT_NSTRING_NOT_EQUAL</CODE></TD> |
| 321 | <TD><CODE>CU_ASSERT_NSTRING_NOT_EQUAL_FATAL</CODE></TD> |
| 322 | </TR> |
| 323 | <TR VALIGN="top"> |
| 324 | <TD><CODE>ASSERT_DOUBLE_EQUAL</CODE></TD> |
| 325 | <TD><CODE>CU_ASSERT_DOUBLE_EQUAL_FATAL</CODE></TD> |
| 326 | </TR> |
| 327 | <TR VALIGN="top"> |
| 328 | <TD><CODE>ASSERT_DOUBLE_NOT_EQUAL</CODE></TD> |
| 329 | <TD><CODE>CU_ASSERT_DOUBLE_NOT_EQUAL_FATAL</CODE></TD> |
| 330 | </TR> |
| 331 | </TABLE> |
| 332 | |
| 333 | <DIV CLASS="NAVFOOTER"> |
| 334 | <HR ALIGN="LEFT" WIDTH="100%"> |
| 335 | <TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0"> |
| 336 | <TR> |
| 337 | <TD WIDTH="33%" ALIGN="left" VALIGN="top"><A HREF="introduction.html" ACCESSKEY="P">Prev</A></TD> |
| 338 | <TD WIDTH="34%" ALIGN="center" VALIGN="top"><A HREF="index.html" ACCESSKEY="H" >Home</A></TD> |
| 339 | <TD WIDTH="33%" ALIGN="right" VALIGN="top"><A HREF="test_registry.html" ACCESSKEY="N" >Next</A></TD> |
| 340 | </TR> |
| 341 | <TR> |
| 342 | <TD WIDTH="33%" ALIGN="left" VALIGN="top">Introduction</TD> |
| 343 | <TD WIDTH="34%" ALIGN="center" VALIGN="top"> </TD> |
| 344 | <TD WIDTH="33%" ALIGN="right" VALIGN="top">The Test Registry</TD> |
| 345 | </TR> |
| 346 | </TABLE> |
| 347 | </DIV> |
| 348 | |
| 349 | </BODY> |
| 350 | </HTML> |