/* By default, <cutest.h> provides the main program entry point (function *main()).However,ifthetestsuiteiscomposedofmultiplesourcefiles *whichinclude<cutest.h>,thenthiscausesaproblemofmultiplemain() *definitions.Toavoidthisproblem,#definemacroTEST_NO_MAINinall *compilationunitsbutone.
*/
/* Macro to specify list of unit tests in the suite. *TheunittestimplementationMUSTprovidelistofunittestsitimplements *withthismacro: * *TEST_LIST={ *{"test1_name",test1_func_ptr}, *{"test2_name",test2_func_ptr}, *... *{0} *}; * *Thelistspecifiesnamesofeachtest(mustbeunique)andpointerto *afunctionimplementingit.Thefunctiondoesnottakeanyarguments *andhasnoreturnvalues,i.e.everytestfunctionhastpbecompatible *withthisprototype: * *voidtest_func(void);
*/ #define TEST_LIST conststruct test__ test_list__[]
/* Macros for testing whether an unit test succeeds or fails. These macros *canbeusedarbitrarilyinfunctionsimplementingtheunittests. * *Ifanyconditionfailsthroughoutexecutionofatest,thetestfails. * *TEST_CHECKtakesonlyoneargument(thecondition),TEST_CHECK_allows *alsotospecifyanerrormessagetoprintoutiftheconditionfails. *(Itexpectsprintf-likeformatstringanditsparameters).Themacros *returnnon-zero(conditionpasses)or0(conditionfails). * *Thatcanbeusefulwhenmoreconditionsshouldbecheckedonlyifsome *precedingconditionpasses,asillustratedinthiscodesnippet: * *SomeStruct*ptr=allocate_some_struct(); *if(TEST_CHECK(ptr!=NULL)){ *TEST_CHECK(ptr->member1<100); *TEST_CHECK(ptr->member2>200); *}
*/ #define TEST_CHECK_(cond, ...) \
test_check__((cond), __FILE__, __LINE__, __VA_ARGS__) #define TEST_CHECK(cond) test_check__((cond), __FILE__, __LINE__, "%s", #cond)
#ifdefined(CUTEST_UNIX__) || defined(CUTEST_WIN__) /* Called if anything goes bad in cutest, or if the unit test ends in other *waythenbynormalreturningfromitsfunction(e.g.exceptionorsome
* abnormal child process termination). */ staticvoid test_error__(constchar *fmt, ...)
{
va_list args;
/* Trigger the unit test. If possible (and not suppressed) it starts a child *processwhocallstest_do_run__(),otherwiseitcallstest_do_run__()
* directly. */ staticvoid test_run__(conststruct test__ *test)
{ int failed = 1;
staticvoid test_help__(void)
{
printf("Usage: %s [options] [test...]\n", test_argv0__);
printf("Run the specified unit tests; or if the option '--skip' is used, " "run all\n");
printf("tests in the suite but those listed. By default, if no tests are " "specified\n");
printf("on the command line, all unit tests in the suite are run.\n");
printf("\n");
printf("Options:\n");
printf( " -s, --skip Execute all unit tests but the listed ones\n");
printf(" --no-exec Do not execute unit tests as child " "processes\n");
printf( " --no-summary Suppress printing of test results summary\n");
printf(" -l, --list List unit tests in the suite and exit\n");
printf(" -v, --verbose Enable more verbose output\n");
printf(" --verbose=LEVEL Set verbose level to LEVEL:\n");
printf(" 0 ... Be silent\n");
printf(" 1 ... Output one line per test (and " "summary)\n");
printf(" 2 ... As 1 and failed conditions (this " "is default)\n");
printf(" 3 ... As 1 and all conditions (and " "extended summary)\n");
printf(" --color=WHEN Enable colorized output (WHEN is one of " "'auto', 'always', 'never')\n");
printf(" -h, --help Display this help and exit\n");
printf("\n");
test_list_names__();
}
int main(int argc, char **argv)
{ conststruct test__ **tests = NULL; int i, j, n = 0; int seen_double_dash = 0;
/* Count all test units */
test_count__ = 0; for (i = 0; test_list__[i].func != NULL; i++)
test_count__++;
/* Run the tests */ if (n == 0) { /* Run all tests */ for (i = 0; test_list__[i].func != NULL; i++)
test_run__(&test_list__[i]);
} elseif (!test_skip_mode__) { /* Run the listed tests */ for (i = 0; i < n; i++)
test_run__(tests[i]);
} else { /* Run all tests except those listed */ for (i = 0; test_list__[i].func != NULL; i++) { int want_skip = 0; for (j = 0; j < n; j++) { if (tests[j] == &test_list__[i]) {
want_skip = 1; break;
}
} if (!want_skip)
test_run__(&test_list__[i]);
}
}
/* Write a summary */ if (!test_no_summary__ && test_verbose_level__ >= 1) {
test_print_in_color__(CUTEST_COLOR_DEFAULT_INTENSIVE__, "\nSummary:\n");
if (test_verbose_level__ >= 3) {
printf(" Count of all unit tests: %4d\n", test_count__);
printf(" Count of run unit tests: %4d\n",
test_stat_run_units__);
printf(" Count of failed unit tests: %4d\n",
test_stat_failed_units__);
printf(" Count of skipped unit tests: %4d\n",
test_count__ - test_stat_run_units__);
}
if (test_stat_failed_units__ == 0) {
test_print_in_color__(CUTEST_COLOR_GREEN_INTENSIVE__, " SUCCESS: All unit tests have passed.\n");
} else {
test_print_in_color__(
CUTEST_COLOR_RED_INTENSIVE__, " FAILED: %d of %d unit tests have failed.\n",
test_stat_failed_units__, test_stat_run_units__);
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.