| 1 | <HTML> |
| 2 | <HEAD> |
| 3 | <TITLE>CUnit - Error Handling</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="running_tests.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="index.html" ACCESSKEY="N" >Home</A></TD> |
| 16 | </TR> |
| 17 | </TABLE> |
| 18 | <HR ALIGN="LEFT" WIDTH="100%"> |
| 19 | |
| 20 | <H2>6. Error Handling</H2> |
| 21 | |
| 22 | <H3 ID="synopsis">6.1. Synopsis</H3> |
| 23 | #include <<A HREF="headers/CUError.h">CUnit/CUError.h</A>> |
| 24 | (included automatically by <<A HREF="headers/CUnit.h">CUnit/CUnit.h</A>>) |
| 25 | <PRE> |
| 26 | typedef enum <A HREF="#errorcodes">CU_ErrorCode</A> |
| 27 | CU_ErrorCode <A HREF="#geterror">CU_get_error</A>(void); |
| 28 | const char* <A HREF="#getmsg">CU_get_error_msg</A>(void); |
| 29 | |
| 30 | typedef enum <A HREF="#actioncodes">CU_ErrorAction</A> |
| 31 | void <A HREF="#setaction">CU_set_error_action</A>(CU_ErrorAction action); |
| 32 | CU_ErrorAction <A HREF="#getaction">CU_get_error_action</A>(void); |
| 33 | </PRE> |
| 34 | <P /> |
| 35 | |
| 36 | <H3 ID="errorhandling">6.2. CUnit Error Handling</H3> |
| 37 | Most CUnit functions set an error code indicating the framework |
| 38 | error status. Some functions return the code, while others just |
| 39 | set the code and return some other value. Two functions are |
| 40 | provided for examining the framework error status: |
| 41 | <P CLASS="indent2"> |
| 42 | <CITE ID="geterror">CU_ErrorCode <B>CU_get_error</B>(void)</CITE><BR /> |
| 43 | <CITE ID="getmsg">const char* <B>CU_get_error_msg</B>(void)</CITE> |
| 44 | </P> |
| 45 | The first returns the error code itself, while the second returns a |
| 46 | message describing the error status. The error code is an |
| 47 | <CODE>enum</CODE> of type <CITE>CU_ErrorCode</CITE> defined in |
| 48 | <<A HREF="headers/CUError.h">CUnit/CUError.h</A>>. |
| 49 | The following error code values are defined: |
| 50 | <P CLASS="indent5"> |
| 51 | <TABLE CELLPADDING=2> |
| 52 | <TR> |
| 53 | <TD><B>Error Value</B></TD> |
| 54 | <TD><B>Description</B></TD> |
| 55 | </TR> |
| 56 | <TR> |
| 57 | <TD><CITE>CUE_SUCCESS</CITE></TD> |
| 58 | <TD>No error condition.</TD> |
| 59 | </TR> |
| 60 | <TR> |
| 61 | <TD><CITE>CUE_NOMEMORY</CITE></TD> |
| 62 | <TD>Memory allocation failed.</TD> |
| 63 | </TR> |
| 64 | <TR> |
| 65 | <TD><CITE>CUE_NOREGISTRY</CITE></TD> |
| 66 | <TD>Test registry not initialized.</TD> |
| 67 | </TR> |
| 68 | <TR> |
| 69 | <TD><CITE>CUE_REGISTRY_EXISTS</CITE></TD> |
| 70 | <TD>Attempt to CU_set_registry() without CU_cleanup_registry().</TD> |
| 71 | </TR> |
| 72 | <TR> |
| 73 | <TD><CITE>CUE_NOSUITE</CITE></TD> |
| 74 | <TD>A required CU_pSuite pointer was NULL.</TD> |
| 75 | </TR> |
| 76 | <TR> |
| 77 | <TD><CITE>CUE_NO_SUITENAME</CITE></TD> |
| 78 | <TD>Required CU_Suite name not provided.</TD> |
| 79 | </TR> |
| 80 | <TR> |
| 81 | <TD><CITE>CUE_SINIT_FAILED</CITE></TD> |
| 82 | <TD>Suite initialization failed.</TD> |
| 83 | </TR> |
| 84 | <TR> |
| 85 | <TD><CITE>CUE_SCLEAN_FAILED</CITE></TD> |
| 86 | <TD>Suite cleanup failed.</TD> |
| 87 | </TR> |
| 88 | <TR> |
| 89 | <TD><CITE>CUE_DUP_SUITE</CITE></TD> |
| 90 | <TD>Duplicate suite name not allowed.</TD> |
| 91 | </TR> |
| 92 | <TR> |
| 93 | <TD><CITE>CUE_SUITE_INACTIVE</CITE></TD> |
| 94 | <TD>A test run was requested for an inactive suite.</TD> |
| 95 | </TR> |
| 96 | <TR> |
| 97 | <TD><CITE>CUE_NOTEST</CITE></TD> |
| 98 | <TD>A required CU_pTest of CU_TestFunc pointer was NULL.</TD> |
| 99 | </TR> |
| 100 | <TR> |
| 101 | <TD><CITE>CUE_NO_TESTNAME</CITE></TD> |
| 102 | <TD>Required CU_Test name not provided.</TD> |
| 103 | </TR> |
| 104 | <TR> |
| 105 | <TD><CITE>CUE_DUP_TEST</CITE></TD> |
| 106 | <TD>Duplicate test case name not allowed.</TD> |
| 107 | </TR> |
| 108 | <TR> |
| 109 | <TD><CITE>CUE_TEST_NOT_IN_SUITE</CITE></TD> |
| 110 | <TD>Test is not registered in the specified suite.</TD> |
| 111 | </TR> |
| 112 | <TR> |
| 113 | <TD><CITE>CUE_TEST_INACTIVE</CITE></TD> |
| 114 | <TD>A test run was requested for an inactive test.</TD> |
| 115 | </TR> |
| 116 | <TR> |
| 117 | <TD><CITE>CUE_FOPEN_FAILED</CITE></TD> |
| 118 | <TD>An error occurred opening a file.</TD> |
| 119 | </TR> |
| 120 | <TR> |
| 121 | <TD><CITE>CUE_FCLOSE_FAILED</CITE></TD> |
| 122 | <TD>An error occurred closing a file.</TD> |
| 123 | </TR> |
| 124 | <TR> |
| 125 | <TD><CITE>CUE_BAD_FILENAME</CITE></TD> |
| 126 | <TD>A bad filename was requested (NULL, empty, nonexistent, etc.).</TD> |
| 127 | </TR> |
| 128 | <TR> |
| 129 | <TD><CITE>CUE_WRITE_ERROR</CITE></TD> |
| 130 | <TD>An error occurred during a write to a file.</TD> |
| 131 | </TR> |
| 132 | </TABLE> |
| 133 | </P> |
| 134 | |
| 135 | <H3 ID="erroraction">6.3. Behavior Upon Framework Errors</H3> |
| 136 | The default behavior when an error condition is encountered is for the |
| 137 | error code to be set and execution continued. In this context, failed |
| 138 | assertions are not considered "framework errors". All other error conditions |
| 139 | including suite initialization or cleanup failures, inactive suites or tests |
| 140 | which are run explicitly, etc. are included. There may be times when clients |
| 141 | prefer for a test run to stop on a framework error, or even for the test |
| 142 | application to exit. This behavior can be set by the user, for which the |
| 143 | following functions are provided: |
| 144 | <P CLASS="indent2"> |
| 145 | <CITE ID="setaction">void <B>CU_set_error_action</B>(CU_ErrorAction action)</CITE><BR /> |
| 146 | <CITE ID="getaction">CU_ErrorAction <B>CU_get_error_action</B>(void)</CITE> |
| 147 | </P> |
| 148 | The error action code is an <CODE>enum</CODE> of type <CITE>CU_ErrorAction</CITE> |
| 149 | defined in <<A HREF="headers/CUError.h">CUnit/CUError.h</A>>. |
| 150 | The following error action codes are defined: |
| 151 | <P CLASS="indent5"> |
| 152 | <TABLE CELLPADDING=2> |
| 153 | <TR> |
| 154 | <TD><B>Error Value</B></TD> |
| 155 | <TD><B>Description</B></TD> |
| 156 | </TR> |
| 157 | <TR> |
| 158 | <TD><CITE>CUEA_IGNORE</CITE></TD> |
| 159 | <TD>Run should be continued when an error condition occurs (default)</TD> |
| 160 | </TR> |
| 161 | <TR> |
| 162 | <TD><CITE>CUEA_FAIL</CITE></TD> |
| 163 | <TD>Run should be stopped when an error condition occurs</TD> |
| 164 | </TR> |
| 165 | <TR> |
| 166 | <TD><CITE>CUEA_ABORT</CITE></TD> |
| 167 | <TD>The application should exit() when an error conditions occurs</TD> |
| 168 | </TR> |
| 169 | </TABLE> |
| 170 | |
| 171 | <H3 ID="deprecated">6.4. Deprecated v1 Variables & Functions</H3> |
| 172 | The following variables and functions are deprecated as of version 2. |
| 173 | To use these deprecated names, user code must be compiled with |
| 174 | <CITE>USE_DEPRECATED_CUNIT_NAMES</CITE> defined. |
| 175 | <P /> |
| 176 | <TABLE CELLPADDING=5 BORDER=2> |
| 177 | <TR VALIGN="top"> |
| 178 | <TD><B>Deprecated Name</B></TD> |
| 179 | <TD><B>Equivalent New Name</B></TD> |
| 180 | </TR> |
| 181 | <TR VALIGN="top"> |
| 182 | <TD><CODE>get_error()</CODE></TD> |
| 183 | <TD><A HREF="#getmsg">CU_get_error_msg()</A></TD> |
| 184 | </TR> |
| 185 | <TR VALIGN="top"> |
| 186 | <TD><CODE>error_code</CODE></TD> |
| 187 | <TD>None. Use <A HREF="#getcode">CU_get_error()</A></TD> |
| 188 | </TR> |
| 189 | </TABLE> |
| 190 | |
| 191 | <DIV CLASS="NAVFOOTER"> |
| 192 | <HR ALIGN="LEFT" WIDTH="100%"> |
| 193 | <TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0"> |
| 194 | <TR> |
| 195 | <TD WIDTH="33%" ALIGN="left" VALIGN="top"><A HREF="running_tests.html" ACCESSKEY="P">Prev</A></TD> |
| 196 | <TD WIDTH="34%" ALIGN="center" VALIGN="top"><A HREF="index.html" ACCESSKEY="H" >Home</A></TD> |
| 197 | <TD WIDTH="33%" ALIGN="right" VALIGN="top"> </TD> |
| 198 | </TR> |
| 199 | <TR> |
| 200 | <TD WIDTH="33%" ALIGN="left" VALIGN="top">Running Tests</TD> |
| 201 | <TD WIDTH="34%" ALIGN="center" VALIGN="top"> </TD> |
| 202 | <TD WIDTH="33%" ALIGN="right" VALIGN="top"> </TD> |
| 203 | </TR> |
| 204 | </TABLE> |
| 205 | </DIV> |
| 206 | |
| 207 | </BODY> |
| 208 | </HTML> |
| 209 |