Debian Codegraph @

doc/running_tests.html

raw symbols SCIP
1 <HTML>
2 <HEAD>
3 <TITLE>CUnit - Running Tests</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="managing_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="error_handling.html" ACCESSKEY="N" >Next</A></TD>
16 </TR>
17 </TABLE>
18 <HR ALIGN="LEFT" WIDTH="100%">
19
20 <H2>5. Running Tests</H2>
21
22 <H3 ID="synopsis">5.1. Synopsis</H3>
23 #include &lt;<A HREF="headers/Automated.h">CUnit/Automated.h</A>&gt;
24 <PRE>
25 void <A HREF="#automated">CU_automated_run_tests</A>(void)
26 CU_ErrorCode <A HREF="#automated">CU_list_tests_to_file</A>(void)
27 void <A HREF="#automated">CU_set_output_filename</A>(const char* szFilenameRoot)
28 </PRE>
29 <P />
30
31 #include &lt;<A HREF="headers/basic.h">CUnit/Basic.h</A>&gt;
32 <PRE>
33 typedef enum <A HREF="#basic">CU_BasicRunMode</A>
34 CU_ErrorCode <A HREF="#basic">CU_basic_run_tests</A>(void)
35 CU_ErrorCode <A HREF="#basic">CU_basic_run_suite</A>(CU_pSuite pSuite)
36 CU_ErrorCode <A HREF="#basic">CU_basic_run_test</A>(CU_pSuite pSuite, CU_pTest pTest)
37 void <A HREF="#basic">CU_basic_set_mode</A>(CU_BasicRunMode mode)
38 CU_BasicRunMode <A HREF="#basic">CU_basic_get_mode</A>(void)
39 void <A HREF="#basic">CU_basic_show_failures</A>(CU_pFailureRecord pFailure)
40 </PRE>
41 <P />
42
43 #include &lt;<A HREF="headers/Console.h">CUnit/Console.h</A>&gt;
44 <PRE>
45 void <A HREF="#console">CU_console_run_tests</A>(void)
46 </PRE>
47 <P />
48
49 #include &lt;<A HREF="headers/CUCurses.h">CUnit/CUCurses.h</A>&gt;
50 <PRE>
51 void <A HREF="#curses">CU_curses_run_tests</A>(void)
52 </PRE>
53
54 <P />
55 #include &lt;<A HREF="headers/TestRun.h">CUnit/TestRun.h</A>&gt;
56 (included automatically by &lt;<A HREF="headers/CUnit.h">CUnit/CUnit.h</A>&gt;)
57 <PRE>
58 unsigned int <A HREF="#results">CU_get_number_of_suites_run</A>(void)
59 unsigned int <A HREF="#results">CU_get_number_of_suites_failed</A>(void)
60 unsigned int <A HREF="#results">CU_get_number_of_tests_run</A>(void)
61 unsigned int <A HREF="#results">CU_get_number_of_tests_failed</A>(void)
62 unsigned int <A HREF="#results">CU_get_number_of_asserts</A>(void)
63 unsigned int <A HREF="#results">CU_get_number_of_successes</A>(void)
64 unsigned int <A HREF="#results">CU_get_number_of_failures</A>(void)
65
66 typedef struct <A HREF="#results">CU_RunSummary</A>
67 typedef CU_Runsummary* <A HREF="#results">CU_pRunSummary</A>
68 const CU_pRunSummary <A HREF="#results">CU_get_run_summary</A>(void)
69
70 typedef struct <A HREF="#results">CU_FailureRecord</A>
71 typedef CU_FailureRecord* <A HREF="#results">CU_pFailureRecord</A>
72 const CU_pFailureRecord <A HREF="#results">CU_get_failure_list</A>(void)
73 unsigned int <A HREF="#results">CU_get_number_of_failure_records</A>(void)
74
75 void <A HREF="#modifying-inactive">CU_set_fail_on_inactive</A>(CU_BOOL new_inactive)
76 CU_BOOL <A HREF="#modifying-inactive">CU_get_fail_on_inactive</A>(void)
77 </PRE>
78 <P />
79
80 <H3 ID="overview">5.2. Running Tests in CUnit</H3>
81 CUnit supports running all tests in all registered suites, but individual tests
82 or suites can also be run. During each run, the framework keeps track of the
83 number of suites, tests, and assertions run, passed, and failed. Note that the
84 previous results are cleared each time a test run is initiated (even if it fails).
85 Individual suites or tests can be <A HREF="managing_tests.html#activation">deactivated</A>
86 if the client wishes to exclude them from a particular test run. However, it is
87 a <A HREF="error_handling.html#errorhandling">framework error</A> to deactivate a
88 suite or test and then explicitly request that it be run.
89 <P />
90 While CUnit provides primitive functions for running suites and tests, most
91 users will want to use one of the simplified user interfaces. These
92 interfaces handle the details of interaction with the framework and provide
93 output of test details and results for the user.
94 <P />
95 The following interfaces are included in the CUnit library:
96 <P CLASS="indent5">
97 <TABLE CELLPADDING=3 BORDER=1>
98 <TR>
99 <TD><B>Interface</B></TD>
100 <TD ALIGN="center"><B>Platform</B></TD>
101 <TD><B>Description</B></TD>
102 </TR>
103 <TR>
104 <TD><A HREF="#automated">Automated</A></TD>
105 <TD ALIGN="center">all</TD>
106 <TD>non-interactive with output to xml files</TD>
107 </TR>
108 <TR>
109 <TD><A HREF="#basic">Basic</A></TD>
110 <TD ALIGN="center">all</TD>
111 <TD>non-interactive with optional output to stdout</TD>
112 </TR>
113 <TR>
114 <TD><A HREF="#console">Console</A></TD>
115 <TD ALIGN="center">all</TD>
116 <TD>interactive console mode under user control</TD>
117 </TR>
118 <TR>
119 <TD><A HREF="#curses">Curses</A></TD>
120 <TD ALIGN="center">Linux/Unix</TD>
121 <TD>interactive curses mode under user control</TD>
122 </TR>
123 </TABLE>
124 </P>
125 If these interfaces are not sufficient, clients can also use
126 the primitive framework API defined in
127 &lt;<A HREF="headers/TestRun.h">CUnit/TestRun.h</A>&gt;. See the source
128 code for the various interfaces for examples of how to interact with
129 the primitive API directly.
130
131 <H3 ID="automated">5.3. Automated Mode</H3>
132 The automated interface is non-interactive. Clients initiate a test run,
133 and the results are output to an XML file. A listing of the registered tests
134 and suites can also be reported to an XML file.
135 <P />
136 The following functions comprise the automated interface API:
137
138 <P ID="auto-run" CLASS="indent2"><CITE>void <B>CU_automated_run_tests</B>(void)</CITE></P>
139 <P CLASS="indent5">Runs all tests in all registered (and active) suites. Test results
140 are output to a file named <I>ROOT-Results.xml</I>. The filename <I>ROOT</I> can
141 be set using <A HREF="#auto-setroot">CU_set_output_filename()</A>, or else the default
142 <I>CUnitAutomated-Results.xml</I> is used. Note that if a distict filename <I>ROOT</I>
143 is not set before each run, the results file will be overwritten.
144 <BR /><BR />
145 The results file is supported by both a document type definition
146 file (<I>CUnit-Run.dtd</I>) and XSL stylesheet (<I>CUnit-Run.xsl</I>).
147 These are provided in the <I>Share</I> subdirectory of the source and installation
148 trees.</P>
149
150 <P ID="auto-list" CLASS="indent2"><CITE>CU_ErrorCode <B>CU_list_tests_to_file</B>(void)</CITE></P>
151 <P CLASS="indent5">Lists the registered suites and associated tests to file.
152 The listing file is named <I>ROOT-Listing.xml</I>. The filename <I>ROOT</I>
153 can be set using <A HREF="#auto-setroot">CU_set_output_filename()</A>, or else the default
154 <I>CUnitAutomated</I> is used. Note that if a distict filename <I>ROOT</I>
155 is not set before each run, the listing file will be overwritten.
156 <BR /><BR />
157 The listing file is supported by both a document type definition
158 file (<I>CUnit-List.dtd</I>) and XSL stylesheet (<I>CUnit-List.xsl</I>).
159 These are provided in the <I>Share</I> subdirectory of the source and installation
160 trees.
161 <BR /><BR />
162 Note also that a listing file is not generated automatically by
163 <A HREF="#auto-run">CU_automated_run_tests()</A>. Client code must explicitly request a
164 listing when one is desired.</P>
165
166 <P ID="auto-setroot" CLASS="indent2"><CITE>void <B>CU_set_output_filename</B>(const char* szFilenameRoot)</CITE></P>
167 <P CLASS="indent5">Sets the output filenames for the results and listing files.
168 <I>szFilenameRoot</I> is used to construct the filenames by appending
169 <I>-Results.xml</I> and <I>-Listing.xml</I>, respectively.
170
171 <H3 ID="basic">5.4. Basic Mode</H3>
172 The basic interface is also non-interactive, with results output to stdout. This
173 interface supports running individual suites or tests, and allows client code to
174 control the type of output displayed during each run. This interface provides the
175 most flexibility to clients desiring simplified access to the CUnit API.
176 <P />
177 The following public functions are provided:
178
179 <P ID="basic-runall" CLASS="indent2"><CITE>CU_ErrorCode <B>CU_basic_run_tests</B>(void)</CITE></P>
180 <P CLASS="indent5">Runs all tests in all registered suites. Only active
181 suites are executed, and it is not considered an error if inactive suites
182 are encountered and skipped. Returns the 1st error code occurring during
183 the test run. The type of output is controlled by the current run mode,
184 which can be set using <A HREF="#basic-setmode">CU_basic_set_mode()</A>.
185
186 <P ID="basic-runsuite" CLASS="indent2"><CITE>CU_ErrorCode <B>CU_basic_run_suite</B>(CU_pSuite pSuite)</CITE></P>
187 <P CLASS="indent5">Runs all tests in single specified suite. Returns the 1st
188 error code occurring during the test run. <CITE>CU_basic_run_suite</CITE> itself
189 sets <CITE>CUE_NOSUITE</CITE> if pSuite is <CODE>NULL</CODE> and
190 <CITE>CUE_SUITE_INACTIVE</CITE> if pSuite is not active for execution.
191 The type of output is controlled by the current run mode, which can be set using
192 <A HREF="#basic-setmode">CU_basic_set_mode()</A>.
193
194 <P ID="basic-runtest" CLASS="indent2"><CITE>CU_ErrorCode <B>CU_basic_run_test</B>(CU_pSuite pSuite, CU_pTest pTest)</CITE></P>
195 <P CLASS="indent5">Runs a single test in a specified suite. Returns the 1st
196 error code occurring during the test run. <CITE>CU_basic_run_test</CITE> itself
197 sets <CITE>CUE_NOSUITE</CITE> if pSuite is <CODE>NULL</CODE>, <CITE>CUE_NOTEST</CITE>
198 if pTest is <CODE>NULL</CODE>, <CITE>CUE_SUITE_INACTIVE</CITE> if pSuite is not
199 active, <CITE>CUE_TEST_NOT_IN_SUITE</CITE> if pTest is not a registered test in
200 pSuite, and <CITE>CUE_TEST_INACTIVE</CITE> if pTest is not active. The type of
201 output is controlled by the current run mode, which can be set using
202 <A HREF="#basic-setmode">CU_basic_set_mode()</A>.
203
204 <P ID="basic-setmode" CLASS="indent2"><CITE>void <B>CU_basic_set_mode</B>(CU_BasicRunMode mode)</CITE></P>
205 <P CLASS="indent5">Sets the basic run mode, which controls the output during
206 test runs. Choices are:
207 <P CLASS="indent10">
208 <TABLE CELLPADDING=1>
209 <TR>
210 <TD><CITE>CU_BRM_NORMAL</CITE></TD>
211 <TD>Failures and run summary are printed.</TD>
212 </TR>
213 <TR>
214 <TD><CITE>CU_BRM_SILENT</CITE></TD>
215 <TD>No output is printed except error messages.</TD>
216 </TR>
217 <TR>
218 <TD><CITE>CU_BRM_VERBOSE</CITE></TD>
219 <TD>Maximum output of run details.</TD>
220 </TR>
221 </TABLE>
222 </P>
223
224 <P ID="basic-getmode" CLASS="indent2"><CITE>CU_BasicRunMode <B>CU_basic_get_mode</B>(void)</CITE></P>
225 <P CLASS="indent5">Retrieves the current basic run mode code.
226
227 <P ID="basic-failure" CLASS="indent2"><CITE>void <B>CU_basic_show_failures</B>(CU_pFailureRecord pFailure)</CITE></P>
228 <P CLASS="indent5">Prints a summary of all failures to stdout.
229 Does not depend on the run mode.
230
231 <H3 ID="console">5.5. Interactive Console Mode</H3>
232 The console interface is interactive. All the client needs to do is
233 initiate the console session, and the user controls the test run
234 interactively. This includes selection & running of registered suites
235 and tests, and viewing test results. To start a console session, use
236 <P ID="console-run" CLASS="indent2"><CITE>void <B>CU_console_run_tests</B>(void)</CITE></P>
237
238 <H3 ID="curses">5.6. Interactive Curses Mode</H3>
239 The curses interface is interactive. All the client needs to do is
240 initiate the curses session, and the user controls the test run
241 interactively. This include selection & running of registered suites
242 and tests, and viewing test results. Use of this interface requires
243 linking the ncurses library into the application. To start a curses
244 session, use
245 <P ID="curses-run" CLASS="indent2"><CITE>void <B>CU_curses_run_tests</B>(void)</CITE></P>
246
247 <H3 ID="modifying">5.7. Modifying General Runtime Behavior</H3>
248 The following functions allow the client to modify the behavior of the
249 framework during a test run:
250 <P ID="modifying-inactive" CLASS="indent2"><CITE>
251 void <B>CU_set_fail_on_inactive</B>(CU_BOOL new_inactive)<BR />
252 CU_BOOL <B>CU_get_fail_on_inactive</B>(void)<BR />
253 </CITE></P>
254 <P CLASS="indent5">The default runtime behavior is for inactive suites and
255 tests to be reported as failures. This is so the client is aware that their
256 test structure has been partially deactivated. If the client prefers to have
257 inactive suites and tests ignored, the behavior can be modified using these
258 functions. A value of <CODE>CU_FALSE</CODE> indicates that the framework
259 will ignore inactive entities; <CODE>CU_TRUE</CODE> will have them treated as
260 failures.</P>
261
262 <H3 ID="results">5.8. Getting Test Results</H3>
263 The interfaces present results of test runs, but client code may sometimes need
264 to access the results directly. These results include various run counts, as
265 well as a linked list of failure records holding the failure details. Note that
266 test results are overwritten each time a new test run is started, or when the
267 registry is <A HREF="test_registry.html#init">initialized</A> or
268 <A HREF="test_registry.html#cleanup">cleaned up</A>.
269 <P />
270 Functions for accessing the test results are:
271
272 <P ID="results-counts" CLASS="indent2"><CITE>
273 unsigned int <B>CU_get_number_of_suites_run</B>(void)<BR />
274 unsigned int <B>CU_get_number_of_suites_failed</B>(void)<BR />
275 unsigned int <B>CU_get_number_of_tests_run</B>(void)<BR />
276 unsigned int <B>CU_get_number_of_tests_failed</B>(void)<BR />
277 unsigned int <B>CU_get_number_of_asserts</B>(void)<BR />
278 unsigned int <B>CU_get_number_of_successes</B>(void)<BR />
279 unsigned int <B>CU_get_number_of_failures</B>(void)
280 </CITE></P>
281 <P CLASS="indent5">These functions report the number of suites, tests, and
282 assertions that ran or failed during the last run. A suite is considered failed
283 if its initialization or cleanup function returned non-<CODE>NULL</CODE>, or if
284 it was inactive and the framework was set to <A href="#modifying-inactive">treat
285 inactive suites/tests as failures</A>. A test fails if any of its assertions
286 failed or if it was inactive under the same conditions. The last 3 functions
287 all refer to individual assertions. An inactive suite (or test) is not counted
288 among the number of suites (tests) run. A consequence of this is that a suite
289 (or test) can fail even though it is not reported as having been run.
290 <BR /><BR />
291 To retrieve the total number of registered suites and tests, use
292 <A HREF="test_registry.html#stuct">CU_get_registry()&minus;&gt;uiNumberOfSuites</A>
293 and <A HREF="test_registry.html#stuct">CU_get_registry()&minus;&gt;uiNumberOfTests</A>,
294 respectively.</P>
295
296 <P ID="results-getsummary" CLASS="indent2"><CITE>const CU_pRunSummary <B>CU_get_run_summary</B>(void)</CITE></P>
297 <P CLASS="indent5">Retrieves all test result counts at once. The return value is
298 a pointer to a saved structure containing the counts. This data type is defined in
299 &lt;<A HREF="headers/TestRun.h">CUnit/TestRun.h</A>&gt; (included automatically by
300 &lt;<A HREF="headers/CUnit.h">CUnit/CUnit.h</A>&gt;):
301 </P>
302 <PRE CLASS="indent10">
303 typedef struct CU_RunSummary
304 {
305 unsigned int nSuitesRun;
306 unsigned int nSuitesFailed;
307 unsigned int nTestsRun;
308 unsigned int nTestsFailed;
309 unsigned int nAsserts;
310 unsigned int nAssertsFailed;
311 unsigned int nFailureRecords;
312 } <CITE><B>CU_RunSummary</B></CITE>;
313
314
315 typedef CU_Runsummary* <CITE><B>CU_pRunSummary</B></CITE>;
316 </PRE>
317 <P CLASS="indent5">The structure variable associated with the returned pointer is owned
318 by the framework, so the user should not free or otherwise change it. <I>Note that the
319 pointer may be invalidated once another test run is initiated.</I>
320 </P>
321
322 <P ID="results-getfailures" CLASS="indent2"><CITE>const CU_pFailureRecord <B>CU_get_failure_list</B>(void)</CITE></P>
323 <P CLASS="indent5">Retrieves a linked list recording any failures occurring during the
324 last test run (<CODE>NULL</CODE> for no failures). The data type of the return value
325 is declared in &lt;<A HREF="headers/TestRun.h">CUnit/TestRun.h</A>&gt; (included
326 automatically by &lt;<A HREF="headers/CUnit.h">CUnit/CUnit.h</A>&gt;). Each failure
327 record contains information about the location and nature of the failure:
328 </P>
329 <PRE CLASS="indent10">
330 typedef struct CU_FailureRecord
331 {
332 unsigned int uiLineNumber;
333 char* strFileName;
334 char* strCondition;
335 CU_pTest pTest;
336 CU_pSuite pSuite;
337
338 struct CU_FailureRecord* pNext;
339 struct CU_FailureRecord* pPrev;
340
341 } <CITE><B>CU_FailureRecord</B></CITE>;
342
343 typedef CU_FailureRecord* <CITE><B>CU_pFailureRecord</B></CITE>;
344 </PRE>
345 <P CLASS="indent5">The structure variable associated with the returned pointer is owned
346 by the framework, so the user should not free or otherwise change it. <I>Note that the
347 pointer may be invalidated once another test run is initiated.</I>
348 </P>
349
350 <P ID="results-failurecount" CLASS="indent2"><CITE>
351 unsigned int <B>CU_get_number_of_failure_records</B>(void)<BR />
352 </CITE></P>
353 <P CLASS="indent5">Retrieves the number of <CITE>CU_FailureRecords</CITE> in
354 the linked list of failures returned by <CITE>CU_get_failure_list()</CITE>. Note
355 that this can be more than the number of failed assertions, since suite
356 initialization and cleanup failures are included.</P>
357
358 <H3 ID="deprecated">5.9. Deprecated v1 Data Types & Functions</H3>
359 The following data types and functions are deprecated as of version 2.
360 To use these deprecated names, user code must be compiled with
361 <CITE>USE_DEPRECATED_CUNIT_NAMES</CITE> defined.
362 <P />
363 <TABLE CELLPADDING=5 BORDER=2>
364 <TR VALIGN="top">
365 <TD><B>Deprecated Name</B></TD>
366 <TD><B>Equivalent New Name</B></TD>
367 </TR>
368 <TR VALIGN="top">
369 <TD><CODE>automated_run_tests()</CODE></TD>
370 <TD>
371 <A HREF="#auto-run">CU_automated_run_tests()</A> plus<BR />
372 <A HREF="#auto-list">CU_list_tests_to_file()</A>
373 </TD>
374 </TR>
375 <TR VALIGN="top">
376 <TD><CODE>set_output_filename()</CODE></TD>
377 <TD><A HREF="#auto-setroot">CU_set_output_filename()</A></TD>
378 </TR>
379 <TR VALIGN="top">
380 <TD><CODE>console_run_tests()</CODE></TD>
381 <TD><A HREF="#console-run">CU_console_run_tests()</A></TD>
382 </TR>
383 <TR VALIGN="top">
384 <TD><CODE>curses_run_tests()</CODE></TD>
385 <TD><A HREF="#curses-run">CU_curses_run_tests()</A></TD>
386 </TR>
387 </TABLE>
388
389 <DIV CLASS="NAVFOOTER">
390 <HR ALIGN="LEFT" WIDTH="100%">
391 <TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0">
392 <TR>
393 <TD WIDTH="33%" ALIGN="left" VALIGN="top"><A HREF="managing_tests.html" ACCESSKEY="P">Prev</A></TD>
394 <TD WIDTH="34%" ALIGN="center" VALIGN="top"><A HREF="index.html" ACCESSKEY="H" >Home</A></TD>
395 <TD WIDTH="33%" ALIGN="right" VALIGN="top"><A HREF="error_handling.html" ACCESSKEY="N" >Next</A></TD>
396 </TR>
397 <TR>
398 <TD WIDTH="33%" ALIGN="left" VALIGN="top">Managing Tests & Suites</TD>
399 <TD WIDTH="34%" ALIGN="center" VALIGN="top">&nbsp;</TD>
400 <TD WIDTH="33%" ALIGN="right" VALIGN="top">Error Handling</TD>
401 </TR>
402 </TABLE>
403 </DIV>
404
405 </BODY>
406 </HTML>
407