CUnit Programmers Reference  3.0
TestRun.h
Go to the documentation of this file.
1 /*
2  * CUnit - A Unit testing framework library for C.
3  * Copyright (C) 2001 Anil Kumar
4  * Copyright (C) 2004-2006 Anil Kumar, Jerry St.Clair
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  */
20 
21 /*
22  * Contains Interface to Run tests.
23  *
24  * Aug 2001 Initial implementation. (AK)
25  *
26  * 09/Aug/2001 Contains generic run tests interface which can be used
27  * for any type of frontend interface framework. (AK)
28  *
29  * 24/Nov/2001 Added Handler for Group Initialization failure condition. (AK)
30  *
31  * 05-Aug-2004 New interface. Since these should be internal functions,
32  * no support for deprecated version 1 names provided now,
33  * eliminated global variables for current test & suite,
34  * moved (renamed) _TestResult here from TestDB.h. (JDS)
35  *
36  * 05-Sep-2004 Added internal test interface. (JDS)
37  *
38  * 23-Apr-2006 Moved doxygen comments into header.
39  * Added type marker to CU_FailureRecord.
40  * Added support for tracking inactive suites/tests. (JDS)
41  *
42  * 08-May-2006 Moved CU_print_run_results() functionality from
43  * console/basic test complete handler. (JDS)
44  *
45  * 24-May-2006 Added callbacks for suite start and complete events.
46  * Added tracking/reported of elapsed time. (JDS)
47  */
48 
67 #ifndef CUNIT_TESTRUN_H_SEEN
68 #define CUNIT_TESTRUN_H_SEEN
69 
70 #include "CUnit/CUnit.h"
71 #include "CUnit/CUError.h"
72 #include "CUnit/TestDB.h"
73 #include <stdio.h>
74 
75 #ifdef __cplusplus
76 extern "C" {
77 #endif
78 
80 typedef enum CU_FailureTypes
81 {
89 /* CU_FailureRecord type definition. */
91 typedef struct CU_FailureRecord
92 {
94  unsigned int uiLineNumber;
95  char* strFileName;
96  char* strFunction;
97  char* strCondition;
107 /* CU_RunSummary type definition. */
109 typedef struct CU_RunSummary
110 {
111  char PackageName[50];
112  unsigned int nSuitesRun;
113  unsigned int nSuitesFailed;
114  unsigned int nSuitesInactive;
115  unsigned int nTestsRun;
116  unsigned int nTestsFailed;
117  unsigned int nTestsInactive;
118  unsigned int nAsserts;
119  unsigned int nAssertsFailed;
120  unsigned int nFailureRecords;
121  double ElapsedTime;
122  unsigned int nTestsSkipped;
123  unsigned int nSuitesSkipped;
127 /*--------------------------------------------------------------------
128  * Type Definitions for Message Handlers.
129  *--------------------------------------------------------------------*/
130 typedef void (*CU_SuiteStartMessageHandler)(const CU_pSuite pSuite);
133 typedef void (*CU_TestStartMessageHandler)(const CU_pTest pTest, const CU_pSuite pSuite);
140 typedef void (*CU_TestCompleteMessageHandler)(const CU_pTest pTest, const CU_pSuite pSuite,
141  const CU_pFailureRecord pFailure);
149 typedef void (*CU_TestSkippedMessageHandler)(const CU_pTest pTest, const CU_pSuite pSuite);
157 typedef void (*CU_SuiteCompleteMessageHandler)(const CU_pSuite pSuite,
158  const CU_pFailureRecord pFailure);
166 typedef void (*CU_AllTestsCompleteMessageHandler)(const CU_pFailureRecord pFailure);
173 typedef void (*CU_SuiteInitFailureMessageHandler)(const CU_pSuite pSuite);
178 typedef void (*CU_SuiteCleanupFailureMessageHandler)(const CU_pSuite pSuite);
183 typedef void (*CU_SuiteSkippedMessageHandler)(const CU_pSuite pSuite);
187 /*--------------------------------------------------------------------
188  * Get/Set functions for Message Handlers
189  *--------------------------------------------------------------------*/
190 CU_EXPORT void CU_set_suite_start_handler(CU_SuiteStartMessageHandler pSuiteStartMessage);
192 CU_EXPORT void CU_set_test_start_handler(CU_TestStartMessageHandler pTestStartMessage);
194 CU_EXPORT void CU_set_test_complete_handler(CU_TestCompleteMessageHandler pTestCompleteMessage);
196 CU_EXPORT void CU_set_test_skipped_handler(CU_TestSkippedMessageHandler pTestSkippedMessage);
198 CU_EXPORT void CU_set_suite_complete_handler(CU_SuiteCompleteMessageHandler pSuiteCompleteMessage);
200 CU_EXPORT void CU_set_all_test_complete_handler(CU_AllTestsCompleteMessageHandler pAllTestsCompleteMessage);
202 CU_EXPORT void CU_set_suite_init_failure_handler(CU_SuiteInitFailureMessageHandler pSuiteInitFailureMessage);
204 CU_EXPORT void CU_set_suite_cleanup_failure_handler(CU_SuiteCleanupFailureMessageHandler pSuiteCleanupFailureMessage);
207 CU_EXPORT void CU_set_suite_skipped_handler(CU_SuiteSkippedMessageHandler pSuiteSkipped);
227 /*--------------------------------------------------------------------
228  * Functions for running registered tests and suites.
229  *--------------------------------------------------------------------*/
230 CU_EXPORT CU_ErrorCode CU_run_all_tests(void);
250 CU_EXPORT CU_ErrorCode CU_run_suite(CU_pSuite pSuite);
276 CU_EXPORT CU_ErrorCode CU_run_test(CU_pSuite pSuite, CU_pTest pTest);
305 /*--------------------------------------------------------------------
306  * Functions for setting runtime behavior.
307  *--------------------------------------------------------------------*/
308 CU_EXPORT void CU_set_fail_on_inactive(CU_BOOL new_inactive);
323 CU_EXPORT CU_BOOL CU_get_fail_on_inactive(void);
333 /*--------------------------------------------------------------------
334  * Functions for getting information about the previous test run.
335  *--------------------------------------------------------------------*/
336 CU_EXPORT unsigned int CU_get_number_of_suites_run(void);
338 CU_EXPORT unsigned int CU_get_number_of_suites_failed(void);
340 CU_EXPORT unsigned int CU_get_number_of_suites_inactive(void);
342 CU_EXPORT unsigned int CU_get_number_of_tests_run(void);
344 CU_EXPORT unsigned int CU_get_number_of_tests_failed(void);
346 CU_EXPORT unsigned int CU_get_number_of_tests_inactive(void);
348 CU_EXPORT unsigned int CU_get_number_of_asserts(void);
350 CU_EXPORT unsigned int CU_get_number_of_successes(void);
352 CU_EXPORT unsigned int CU_get_number_of_failures(void);
354 CU_EXPORT unsigned int CU_get_number_of_failure_records(void);
360 CU_EXPORT double CU_get_elapsed_time(void);
368 CU_EXPORT CU_pFailureRecord CU_get_failure_list(void);
382 CU_EXPORT int CU_count_test_failures(CU_pTest pTest);
388 CU_EXPORT int CU_count_suite_failures(CU_pSuite pSuite);
394 CU_EXPORT int CU_count_all_failures(CU_pTestRegistry pRegistry);
400 CU_EXPORT int CU_count_suite_tests(CU_pSuite pSuite);
406 CU_EXPORT int CU_count_all_tests(CU_pTestRegistry pRegistry);
412 CU_EXPORT CU_pRunSummary CU_get_run_summary(void);
422 CU_EXPORT char * CU_get_run_results_string(void);
437 CU_EXPORT void CU_print_run_results(FILE *file);
449 CU_EXPORT double CU_get_test_duration(CU_pTest pTest);
454 CU_EXPORT double CU_get_suite_duration(CU_pSuite pSuite);
459 /*--------------------------------------------------------------------
460  * Functions for internal & testing use.
461  *--------------------------------------------------------------------*/
462 CU_EXPORT CU_pSuite CU_get_current_suite(void);
464 CU_EXPORT CU_pTest CU_get_current_test(void);
466 CU_EXPORT CU_BOOL CU_is_test_running(void);
471 CU_EXPORT void CU_clear_previous_results(void);
480 CU_EXPORT CU_BOOL CU_assertImplementation(CU_BOOL bValue,
481  unsigned int uiLine,
482  const char *strCondition,
483  const char *strFile,
484  const char *strFunction,
485  CU_BOOL bFatal);
501 CU_EXPORT void CU_SkipImplementation(CU_BOOL bValue,
502  unsigned int uiLine,
503  const char *strCondition,
504  const char *strFile,
505  const char *strFunction);
517 #define CU_SKIP_IF(value) \
518  { CU_SkipImplementation((value), __LINE__, ("CU_SKIP_IF(" #value ")"), __FILE__, CU_FUNC); }
519 
520 
521 
522 #ifdef USE_DEPRECATED_CUNIT_NAMES
525 #endif /* USE_DEPRECATED_CUNIT_NAMES */
526 
527 #ifdef CUNIT_BUILD_TESTS
528 void test_cunit_TestRun(void);
529 #endif
530 
531 #ifdef __cplusplus
532 }
533 #endif
534 #endif /* CUNIT_TESTRUN_H_SEEN */
Error handling functions (user interface).
Management functions for tests, suites, and the test registry.
CU_EXPORT unsigned int CU_get_number_of_successes(void)
Retrieves the number of successful assertions during the last run (reset each run).
Definition: TestRun.c:332
CU_EXPORT void CU_set_test_skipped_handler(CU_TestSkippedMessageHandler pTestSkippedMessage)
Sets the message handler to call when a test is skipped.
Definition: TestRun.c:235
CU_EXPORT void CU_print_run_results(FILE *file)
Prints a summary of the current run results to file.
Definition: TestRun.c:656
CU_EXPORT double CU_get_suite_duration(CU_pSuite pSuite)
Definition: TestRun.c:285
CU_EXPORT void CU_set_suite_init_failure_handler(CU_SuiteInitFailureMessageHandler pSuiteInitFailureMessage)
Sets the message handler to call when a suite initialization function returns an error.
Definition: TestRun.c:259
CU_EXPORT CU_pFailureRecord CU_get_failure_list(void)
Retrieves the head of the linked list of failures which occurred during the last run (reset each run)...
Definition: TestRun.c:368
CU_EXPORT void CU_set_suite_cleanup_failure_handler(CU_SuiteCleanupFailureMessageHandler pSuiteCleanupFailureMessage)
Sets the message handler to call when a suite cleanup function returns an error.
Definition: TestRun.c:267
CU_EXPORT int CU_count_test_failures(CU_pTest pTest)
Count the number of failures from the given test.
Definition: TestRun.c:373
CU_pFailureRecord PTestResult
Definition: TestRun.h:524
CU_EXPORT unsigned int CU_get_number_of_failures(void)
Retrieves the number of failed assertions during the last run (reset each run).
Definition: TestRun.c:338
CU_EXPORT void CU_set_suite_complete_handler(CU_SuiteCompleteMessageHandler pSuiteCompleteMessage)
Sets the message handler to call after each suite is run.
Definition: TestRun.c:243
CU_EXPORT void CU_set_test_start_handler(CU_TestStartMessageHandler pTestStartMessage)
Sets the message handler to call before each test is run.
Definition: TestRun.c:219
void(* CU_SuiteStartMessageHandler)(const CU_pSuite pSuite)
Message handler called at the start of a suite.
Definition: TestRun.h:130
CU_EXPORT void CU_set_fail_on_inactive(CU_BOOL new_inactive)
Sets whether an inactive suite or test is treated as a failure.
Definition: TestRun.c:644
CU_EXPORT CU_ErrorCode CU_run_all_tests(void)
Runs all tests in all suites registered in the test registry.
Definition: TestRun.c:472
CU_EXPORT unsigned int CU_get_number_of_asserts(void)
Retrieves the number of assertions processed during the last run (reset each run).
Definition: TestRun.c:326
CU_EXPORT CU_ErrorCode CU_run_suite(CU_pSuite pSuite)
Runs all tests in a specified suite.
Definition: TestRun.c:508
CU_EXPORT CU_SuiteStartMessageHandler CU_get_suite_start_handler(void)
Retrieves the message handler called before each suite is run.
struct CU_RunSummary CU_RunSummary
Data type for holding statistics and assertion failures for a test run.
CU_EXPORT char * CU_get_run_results_string(void)
Creates a string and fills it with a summary of the current run results.
Definition: TestRun.c:672
CU_EXPORT CU_pSuite CU_get_current_suite(void)
Retrieves a pointer to the currently-running suite (NULL if none).
Definition: TestRun.c:626
CU_EXPORT void CU_set_all_test_complete_handler(CU_AllTestsCompleteMessageHandler pAllTestsCompleteMessage)
Sets the message handler to call after all tests have been run.
Definition: TestRun.c:251
enum CU_FailureTypes CU_FailureType
Types of failures occurring during test runs.
CU_EXPORT void CU_set_test_complete_handler(CU_TestCompleteMessageHandler pTestCompleteMessage)
Sets the message handler to call after each test is run.
Definition: TestRun.c:227
CU_EXPORT unsigned int CU_get_number_of_suites_run(void)
Retrieves the number of suites completed during the previous run (reset each run).
Definition: TestRun.c:290
void(* CU_TestSkippedMessageHandler)(const CU_pTest pTest, const CU_pSuite pSuite)
Message handler called when a test is skipped.
Definition: TestRun.h:149
CU_EXPORT void CU_set_suite_start_handler(CU_SuiteStartMessageHandler pSuiteStartMessage)
Sets the message handler to call before each suite is run.
Definition: TestRun.c:211
CU_ErrorCode
CUnit error codes.
Definition: CUError.h:62
CU_EXPORT unsigned int CU_get_number_of_tests_run(void)
Retrieves the number of tests completed during the previous run (reset each run).
Definition: TestRun.c:308
CU_EXPORT double CU_get_elapsed_time(void)
Retrieves the elapsed time for the last run in seconds (reset each run).
Definition: TestRun.c:357
CU_EXPORT CU_ErrorCode CU_run_test(CU_pSuite pSuite, CU_pTest pTest)
Runs a specific test in a specified suite.
Definition: TestRun.c:535
void(* CU_TestStartMessageHandler)(const CU_pTest pTest, const CU_pSuite pSuite)
Message handler called at the start of a test.
Definition: TestRun.h:133
struct CU_FailureRecord CU_FailureRecord
Data type for holding assertion failure information (linked list).
CU_EXPORT unsigned int CU_get_number_of_tests_failed(void)
Retrieves the number of tests containing failed assertions during the previous run (reset each run).
Definition: TestRun.c:314
CU_EXPORT int CU_count_all_failures(CU_pTestRegistry pRegistry)
Count the number of failed tests overall.
Definition: TestRun.c:421
CU_EXPORT CU_TestCompleteMessageHandler CU_get_test_complete_handler(void)
Retrieves the message handler called after each test is run.
void(* CU_SuiteInitFailureMessageHandler)(const CU_pSuite pSuite)
Message handler called when a suite initializer fails.
Definition: TestRun.h:173
CU_EXPORT CU_pTest CU_get_current_test(void)
Retrievea a pointer to the currently-running test (NULL if none).
Definition: TestRun.c:632
CU_EXPORT CU_SuiteInitFailureMessageHandler CU_get_suite_init_failure_handler(void)
Retrieves the message handler called when a suite initialization error occurs.
CU_EXPORT unsigned int CU_get_number_of_tests_inactive(void)
Retrieves the number of inactive tests found during the previous run (reset each run).
Definition: TestRun.c:320
CU_EXPORT void CU_SkipImplementation(CU_BOOL bValue, unsigned int uiLine, const char *strCondition, const char *strFile, const char *strFunction)
Skip Implementation Called to skip execution of current test or current suite.
Definition: TestRun.c:169
CU_EXPORT CU_SuiteCleanupFailureMessageHandler CU_get_suite_cleanup_failure_handler(void)
Retrieves the message handler called when a suite cleanup error occurs.
CU_EXPORT void CU_set_suite_skipped_handler(CU_SuiteSkippedMessageHandler pSuiteSkipped)
Sets the message handler to call when a suite is skipped.
Definition: TestRun.c:274
CU_EXPORT unsigned int CU_get_number_of_failure_records(void)
Retrieves the number failure records created during the previous run (reset each run).
Definition: TestRun.c:344
CU_EXPORT CU_TestSkippedMessageHandler CU_get_test_skipped_handler(void)
Retrieves the message handler called when a test is skipped.
CU_EXPORT CU_TestStartMessageHandler CU_get_test_start_handler(void)
Retrieves the message handler called before each test is run.
CU_EXPORT int CU_count_suite_failures(CU_pSuite pSuite)
Count the number of failed tests in a suite.
Definition: TestRun.c:395
void(* CU_AllTestsCompleteMessageHandler)(const CU_pFailureRecord pFailure)
Message handler called at the completion of a test run.
Definition: TestRun.h:166
CU_EXPORT CU_AllTestsCompleteMessageHandler CU_get_all_test_complete_handler(void)
Retrieves the message handler called after all tests are run.
CU_EXPORT unsigned int CU_get_number_of_suites_inactive(void)
Retrieves the number of inactive suites found during the previous run (reset each run).
Definition: TestRun.c:302
CU_EXPORT double CU_get_test_duration(CU_pTest pTest)
Definition: TestRun.c:281
CU_EXPORT int CU_count_all_tests(CU_pTestRegistry pRegistry)
Count the number of tests in a suite.
Definition: TestRun.c:408
CU_EXPORT CU_SuiteCompleteMessageHandler CU_get_suite_complete_handler(void)
Retrieves the message handler called after each suite is run.
void(* CU_SuiteSkippedMessageHandler)(const CU_pSuite pSuite)
Message handler called when a suite is skipped during setup.
Definition: TestRun.h:183
void(* CU_TestCompleteMessageHandler)(const CU_pTest pTest, const CU_pSuite pSuite, const CU_pFailureRecord pFailure)
Message handler called at the completion of a test.
Definition: TestRun.h:140
CU_EXPORT CU_BOOL CU_get_fail_on_inactive(void)
Retrieves the current setting for whether inactive suites/tests are treated as failures.
Definition: TestRun.c:650
CU_RunSummary * CU_pRunSummary
Pointer to CU_RunSummary.
Definition: TestRun.h:125
CU_EXPORT void CU_clear_previous_results(void)
Initializes the run summary information stored from the previous test run.
Definition: TestRun.c:620
void(* CU_SuiteCleanupFailureMessageHandler)(const CU_pSuite pSuite)
Message handler called when a suite cleanup function fails.
Definition: TestRun.h:178
CU_FailureTypes
Types of failures occurring during test runs.
Definition: TestRun.h:81
CU_EXPORT unsigned int CU_get_number_of_suites_failed(void)
Retrieves the number of suites which failed to initialize during the previous run (reset each run).
Definition: TestRun.c:296
CU_EXPORT CU_pFailureRecord CU_iterate_test_failures(CU_pTest test, CU_pFailureRecord previous)
Iterate over the recorded failure records of a given test.
Definition: TestRun.c:434
CU_EXPORT CU_BOOL CU_is_test_running(void)
Returns CU_TRUE if a test run is in progress, CU_TRUE otherwise.
Definition: TestRun.c:638
CU_EXPORT CU_BOOL CU_assertImplementation(CU_BOOL bValue, unsigned int uiLine, const char *strCondition, const char *strFile, const char *strFunction, CU_BOOL bFatal)
Assertion implementation function.
Definition: TestRun.c:139
void(* CU_SuiteCompleteMessageHandler)(const CU_pSuite pSuite, const CU_pFailureRecord pFailure)
Message handler called at the completion of a suite.
Definition: TestRun.h:157
CU_FailureRecord _TestResult
Definition: TestRun.h:523
CU_EXPORT CU_pRunSummary CU_get_run_summary(void)
Count the number of tests in all suites.
Definition: TestRun.c:466
CU_FailureRecord * CU_pFailureRecord
Pointer to CU_FailureRecord.
Definition: TestRun.h:105
@ CUF_SuiteInactive
Inactive suite was run.
Definition: TestRun.h:82
@ CUF_AssertFailed
CUnit assertion failed during test run.
Definition: TestRun.h:86
@ CUF_SuiteInitFailed
Suite initialization function failed.
Definition: TestRun.h:83
@ CUF_SuiteCleanupFailed
Suite cleanup function failed.
Definition: TestRun.h:84
@ CUF_TestInactive
Inactive test was run.
Definition: TestRun.h:85
Data type for holding assertion failure information (linked list).
Definition: TestRun.h:92
unsigned int uiLineNumber
Line number of failure.
Definition: TestRun.h:94
char * strFileName
Name of file where failure occurred.
Definition: TestRun.h:95
struct CU_FailureRecord * pPrev
Pointer to previous record in linked list.
Definition: TestRun.h:102
CU_FailureType type
Failure type.
Definition: TestRun.h:93
char * strCondition
Test condition which failed.
Definition: TestRun.h:97
CU_pSuite pSuite
Suite containing test having failure.
Definition: TestRun.h:99
char * strFunction
Function of failure.
Definition: TestRun.h:96
CU_pTest pTest
Test containing failure.
Definition: TestRun.h:98
struct CU_FailureRecord * pNext
Pointer to next record in linked list.
Definition: TestRun.h:101
Data type for holding statistics and assertion failures for a test run.
Definition: TestRun.h:110
unsigned int nSuitesInactive
Number of suites which were inactive.
Definition: TestRun.h:114
unsigned int nSuitesSkipped
Number of suites skipped during execution.
Definition: TestRun.h:123
unsigned int nTestsRun
Number of tests completed during run.
Definition: TestRun.h:115
unsigned int nFailureRecords
Number of failure records generated.
Definition: TestRun.h:120
unsigned int nTestsFailed
Number of tests containing failed assertions.
Definition: TestRun.h:116
unsigned int nSuitesFailed
Number of suites for which initialization failed.
Definition: TestRun.h:113
unsigned int nAsserts
Number of assertions tested during run.
Definition: TestRun.h:118
double ElapsedTime
Elapsed time for run in seconds.
Definition: TestRun.h:121
unsigned int nAssertsFailed
Number of failed assertions.
Definition: TestRun.h:119
unsigned int nTestsSkipped
Number of tests skipped during execution.
Definition: TestRun.h:122
unsigned int nSuitesRun
Number of suites completed during run.
Definition: TestRun.h:112
unsigned int nTestsInactive
Number of tests which were inactive (in active suites).
Definition: TestRun.h:117
CUnit suite data type.
Definition: TestDB.h:162
CUnit test registry data type.
Definition: TestDB.h:237
CUnit test case data type.
Definition: TestDB.h:110