/** Runs all tests within the basic interface. * If non-NULL, the test registry is changed to the specified registry * before running the tests, and reset to the original registry when * done. If NULL, the default CUnit test registry will be used. * @param pRegistry The CU_pTestRegistry containing the tests * to be run. If NULL, use the default registry. * @return An error code indicating the error status * during the test run. */ static CU_ErrorCode basic_run_all_tests(CU_pTestRegistry pRegistry) { CU_pTestRegistry pOldRegistry = NULL; CU_ErrorCode result; f_pRunningSuite = NULL; if (NULL != pRegistry) pOldRegistry = CU_set_registry(pRegistry); result = CU_run_all_tests(); if (NULL != pRegistry) CU_set_registry(pOldRegistry); return result; }
/** Run the registered tests using the automated interface. * If non-NULL. the specified registry is set as the active * registry for running the tests. If NULL, then the default * CUnit test registry is used. The actual test running is * performed by CU_run_all_tests(). * @param pRegistry The test registry to run. */ static void automated_run_all_tests(CU_pTestRegistry pRegistry) { CU_pTestRegistry pOldRegistry = NULL; assert(NULL != f_pTestResultFile); f_pRunningSuite = NULL; if (NULL != pRegistry) { pOldRegistry = CU_set_registry(pRegistry); } fprintf(f_pTestResultFile," <CUNIT_RESULT_LISTING> \n"); CU_run_all_tests(); if (NULL != pRegistry) { CU_set_registry(pOldRegistry); } }
/** Run all tests within the curses interface. * The test registry is changed to the specified registry * before running the tests, and reset to the original * registry when done. * @param pRegistry The CU_pTestRegistry containing the tests * to be run (non-NULL). * @return An error code indicating the error status * during the test run. */ static CU_ErrorCode curses_run_all_tests(CU_pTestRegistry pRegistry) { CU_pTestRegistry pOldRegistry = NULL; CU_ErrorCode result; assert(pRegistry); reset_run_parameters(); f_uiTotalTests = pRegistry->uiNumberOfTests; f_uiTotalSuites = pRegistry->uiNumberOfSuites; if (NULL != pRegistry) { pOldRegistry = CU_set_registry(pRegistry); } result = CU_run_all_tests(); if (NULL != pOldRegistry) { CU_set_registry(pOldRegistry); } return result; }
int main(int argc, char * const *argv) { int ret = 0; #ifdef HAVE_GETOPT_LONG int c; struct option long_options[] = { {"help", 0, 0, 'h'}, {"list", 0, 0, 'l'}, {0, 0, 0, 0} }; while ((c = getopt_long(argc, argv, "hl", long_options, NULL)) != -1) { switch (c) { case 'h': fprintf(stderr, "Usage: %s [test name]\n", argv[0]); fputs("\nOptions:\n", stderr); fputs(" -h, --help Show this help message and exit\n", stderr); fputs(" -l, --list List available tests and exit\n", stderr); exit(EXIT_SUCCESS); case 'l': fputs("Available test suites:\n", stdout);; printSuites(CU_get_registry()->pSuite, stdout); exit(EXIT_SUCCESS); } } if (optind < argc) { CU_pTestRegistry old_registry = CU_get_registry(); CU_set_registry(CU_create_new_registry()); while (optind < argc) { char *suite_name = strdup(argv[optind++]); char *test_name = strstr(suite_name, "::"); if (test_name) { *test_name = '\0'; test_name += 2; } if (copySuite(old_registry, suite_name, test_name) == 0) { fprintf(stderr, "Can't find test suite \"%s", suite_name); if (test_name) fprintf(stderr, "::%s\"\n", test_name); else fputs("\"\n", stderr); exit(EXIT_FAILURE); } free(suite_name); } CU_destroy_existing_registry(&old_registry); } #endif CU_basic_set_mode(CU_BRM_VERBOSE); CU_set_output_filename(XML_TEST_REPORT); CU_basic_run_tests(); ret = (CU_get_number_of_tests_failed() == 0) ? 0 : 1; CU_cleanup_registry(); return ret; }
CU_BOOL CU_check_for_single_runs(void) { CU_pTestRegistry old_reg; CU_pSuite current_suite, active_suite; CU_pTest current_test; unsigned int i, j; CU_BOOL singleRunsFound = CU_TRUE; /* create a new registry and save our old one */ old_reg = CU_set_registry(CU_create_new_registry()); /* go through all suites and tests in the old registry looking for the marker */ current_suite = old_reg->pSuite; for (i = 0; i < old_reg->uiNumberOfSuites; i++) { active_suite = NULL; /* see if the current suite has the marker */ if (strstr(current_suite->pName, "[x]") == NULL) { /* marker was not found so move through the tests */ current_test = current_suite->pTest; for (j = 0; j < current_suite->uiNumberOfTests; j++) { /* see if the current test has the marker */ if (strstr(current_test->pName, "[x]") != NULL) { /* add the current suite if required */ if (active_suite == NULL) { active_suite = CU_add_suite(current_suite->pName, current_suite->pInitializeFunc, current_suite->pCleanupFunc); } /* now add the current test to the active suite */ CU_add_test(active_suite, current_test->pName, current_test->pTestFunc); } /* go to the next test */ current_test = current_test->pNext; } } else { /* marker was found so add the entire current suite */ active_suite = CU_add_suite(current_suite->pName, current_suite->pInitializeFunc, current_suite->pCleanupFunc); current_test = current_suite->pTest; for (j = 0; j < current_suite->uiNumberOfTests; j++) { /* add the current test to the active suite */ CU_add_test(active_suite, current_test->pName, current_test->pTestFunc); /* go to the next test */ current_test = current_test->pNext; } } /* go to the next suite */ current_suite = current_suite->pNext; } /* if no single runs were found then restore the old registery */ if (CU_get_registry()->uiNumberOfSuites == 0) { old_reg = CU_set_registry(old_reg); singleRunsFound = CU_FALSE; } /* destroy the old registry */ CU_destroy_existing_registry(&old_reg); return singleRunsFound; }