⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testrun.c

📁 CUNIT
💻 C
📖 第 1 页 / 共 5 页
字号:
    /* Clear results from the previous run */    clear_previous_results(&f_run_summary, &f_failure_list);    pSuite = pRegistry->pSuite;    while (pSuite && (!result || (CU_get_error_action() == CUEA_IGNORE))) {      /* if the suite has tests, run it */      if (pSuite->uiNumberOfTests) {        result2 = run_single_suite(pSuite, &f_run_summary);        result = (CUE_SUCCESS == result) ? result2 : result;  /* result = 1st error encountered */      }      pSuite = pSuite->pNext;    }    /* test run is complete - clear flag */    f_bTestIsRunning = FALSE;    if (f_pAllTestsCompleteMessageHandler)     (*f_pAllTestsCompleteMessageHandler)(f_failure_list);  }  return result;}/*------------------------------------------------------------------------*//** Run all tests in a specified suite. * The suite need not be registered in the test registry to be run. * Any initialization function for the suite is first called, * then the suite is run using run_single_suite(), and any * suite cleanup function is called.  Note that the * run statistics (counts of tests, successes, failures) * are initialized each time this function is called. * If an error condition occurs during the run, the action * depends on the current error action (see CU_set_error_action()). * @param pSuite The suite containing the test (non-NULL) * @return A CU_ErrorCode indicating the first error condition *         encountered while running the suite.  CU_run_suite() *         sets and returns CUE_NOSUITE if pSuite is NULL.  Other *         error codes can be set during suite initialization or *         cleanup or during test runs. * @see CU_run_all_tests() to run all suites. * @see CU_run_test() to run a single test in a specific suite. */CU_ErrorCode CU_run_suite(CU_pSuite pSuite){  CU_ErrorCode result;  CU_set_error(result = CUE_SUCCESS);  if (!pSuite) {    CU_set_error(result = CUE_NOSUITE);  }  else {    /* test run is starting - set flag */    f_bTestIsRunning = TRUE;    /* Clear results from the previous run */    clear_previous_results(&f_run_summary, &f_failure_list);    if (pSuite->uiNumberOfTests)      result = run_single_suite(pSuite, &f_run_summary);    /* test run is complete - clear flag */    f_bTestIsRunning = FALSE;    if (f_pAllTestsCompleteMessageHandler)      (*f_pAllTestsCompleteMessageHandler)(f_failure_list);  }  return result;}/*------------------------------------------------------------------------*//** Run a specific test in a specified suite. * The suite need not be registered in the test registry to be run, * although the test must be registered in the specified suite. * Any initialization function for the suite is first * called, then the test is run using run_single_test(), and * any suite cleanup function is called.  Note that the * run statistics (counts of tests, successes, failures) * are initialized each time this function is called. * @param pSuite The suite containing the test (non-NULL) * @param pTest  The test to run (non-NULL) * @return A CU_ErrorCode indicating the first error condition *         encountered while running the suite.  CU_run_test() *         sets and returns CUE_NOSUITE if pSuite is NULL, *         CUE_NOTEST if pTest is NULL, and CUE_TEST_NOT_IN_SUITE *         if pTest is not registered in pSuite.  Other *         error codes can be set during suite initialization or *         cleanup or during the test run. * @see CU_run_all_tests() to run all tests/suites. * @see CU_run_suite() to run all tests in a specific suite. */CU_ErrorCode CU_run_test(CU_pSuite pSuite, CU_pTest pTest){  CU_ErrorCode result;  CU_ErrorCode result2;  CU_set_error(result = CUE_SUCCESS);  if (!pSuite) {    CU_set_error(result = CUE_NOSUITE);  }  else if (!pTest) {    CU_set_error(result = CUE_NOTEST);  }  else if (NULL == CU_get_test_by_name(pTest->pName, pSuite)) {    CU_set_error(result = CUE_TEST_NOT_IN_SUITE);  }  else {    /* test run is starting - set flag */    f_bTestIsRunning = TRUE;    /* Clear results from the previous run */    clear_previous_results(&f_run_summary, &f_failure_list);    f_pCurTest = NULL;    f_pCurSuite = pSuite;    if ((pSuite->pInitializeFunc) && (*pSuite->pInitializeFunc)()) {      if (f_pSuiteInitFailureMessageHandler) {        (*f_pSuiteInitFailureMessageHandler)(pSuite);      }      f_run_summary.nSuitesFailed++;      add_failure(&f_failure_list, &f_run_summary,                  0, "Suite Initialization failed - Test Skipped", "CUnit System", pSuite, pTest);      CU_set_error(result = CUE_SINIT_FAILED);      /* test run is complete - clear flag */      f_bTestIsRunning = FALSE;    }    /* reach here if no suite initialization, or if it succeeded */    else {      result2 = run_single_test(pTest, &f_run_summary);      result = (CUE_SUCCESS == result) ? result2 : result;      if ((pSuite->pCleanupFunc) && (*pSuite->pCleanupFunc)()) {        f_run_summary.nSuitesFailed++;        add_failure(&f_failure_list, &f_run_summary,                    0, "Suite cleanup failed.", "CUnit System", pSuite, pTest);        result = (CUE_SUCCESS == result) ? CUE_SCLEAN_FAILED : result;        CU_set_error(CUE_SCLEAN_FAILED);      }      /* test run is complete - clear flag */      f_bTestIsRunning = FALSE;      if (f_pAllTestsCompleteMessageHandler)        (*f_pAllTestsCompleteMessageHandler)(f_failure_list);      f_pCurSuite = NULL;    }  }  return result;}/*------------------------------------------------------------------------*//** Initialize the run summary information stored from * the previous test run.  Resets the run counts to zero, * and frees any memory associated with failure records. * Calling this function multiple times, while inefficient, * will not cause an error condition. * @see clear_previous_results() */void CU_clear_previous_results(void){  clear_previous_results(&f_run_summary, &f_failure_list);}/*------------------------------------------------------------------------*//** Retrieve a pointer to the currently-running suite (NULL if none). */CU_pSuite CU_get_current_suite(void){  return f_pCurSuite;}/*------------------------------------------------------------------------*//** Retrieve a pointer to the currently-running test (NULL if none). */CU_pTest CU_get_current_test(void){  return f_pCurTest;}/*------------------------------------------------------------------------*//** Returns <CODE>TRUE</CODE> if a test run is in progress, * <CODE>TRUE</CODE> otherwise. */BOOL CU_is_test_running(void){  return f_bTestIsRunning;}/*------------------------------------------------------------------------*//** Record a failed test. * This function is called whenever a test fails to record the * details of the failure.  This includes user assertion failures * and system errors such as failure to initialize a suite. * @param ppFailure    Pointer to head of linked list of failure *                     records to append with new failure record. *                     If NULL, it will be set to point to the new *                     failure record. * @param pRunSummary  Pointer to CU_RunSummary keeping track of failure records *                     (ignored if NULL). * @param uiLineNumber Line number of the failure, if applicable. * @param szCondition  Description of failure condition * @param szFileName   Name of file, if applicable * @param pSuite       The suite being run at time of failure * @param pTest        The test being run at time of failure */void add_failure(CU_pFailureRecord* ppFailure, CU_pRunSummary pRunSummary,                 unsigned int uiLineNumber, char szCondition[],                 char szFileName[], CU_pSuite pSuite, CU_pTest pTest){  CU_pFailureRecord pFailureNew = NULL;  CU_pFailureRecord pTemp = NULL;  pFailureNew = (CU_pFailureRecord)CU_MALLOC(sizeof(CU_FailureRecord));  if (!pFailureNew)    return;  pFailureNew->strFileName = NULL;  pFailureNew->strCondition = NULL;  if (szFileName) {    pFailureNew->strFileName = (char*)CU_MALLOC(strlen(szFileName) + 1);    if(!pFailureNew->strFileName) {      CU_FREE(pFailureNew);      return;    }    strcpy(pFailureNew->strFileName, szFileName);  }  if (szCondition) {    pFailureNew->strCondition = (char*)CU_MALLOC(strlen(szCondition) + 1);    if (!pFailureNew->strCondition) {      if(pFailureNew->strFileName) {        CU_FREE(pFailureNew->strFileName);      }      CU_FREE(pFailureNew);      return;    }    strcpy(pFailureNew->strCondition, szCondition);  }  pFailureNew->uiLineNumber = uiLineNumber;  pFailureNew->pTest = pTest;  pFailureNew->pSuite = pSuite;  pFailureNew->pNext = NULL;  pFailureNew->pPrev = NULL;  pTemp = *ppFailure;  if (pTemp) {    while (pTemp->pNext) {      pTemp = pTemp->pNext;    }    pTemp->pNext = pFailureNew;    pFailureNew->pPrev = pTemp;  }  else {    *ppFailure = pFailureNew;  }  if (NULL != pRunSummary)    ++(pRunSummary->nFailureRecords);  f_last_failure = pFailureNew;}/* *  Local function for result set initialization/cleanup. *//*------------------------------------------------------------------------*//** Initialize the run summary information in the * specified structure.  Resets the run counts to zero, * and calls cleanup_failure_list() if failures * were recorded by the last test run. * Calling this function multiple times, while inefficient, * will not cause an error condition. * @param pRunSummary CU_RunSummary to initialize. * @see CU_clear_previous_results() */static void clear_previous_results(CU_pRunSummary pRunSummary, CU_pFailureRecord* ppFailure){  pRunSummary->nSuitesRun = 0;  pRunSummary->nSuitesFailed = 0;  pRunSummary->nTestsRun = 0;  pRunSummary->nTestsFailed = 0;  pRunSummary->nAsserts = 0;  pRunSummary->nAssertsFailed = 0;  pRunSummary->nFailureRecords = 0;  if (NULL != *ppFailure)    cleanup_failure_list(ppFailure);  f_last_failure = NULL;}/*------------------------------------------------------------------------*//** Free all memory allocated for the linked list of * test failure records.  pFailure is reset to NULL * after its list is cleaned up. * @param ppFailure Pointer to head of linked list of *                  CU_pFailureRecords to clean. * @see CU_clear_previous_results() */static void cleanup_failure_list(CU_pFailureRecord* ppFailure){  CU_pFailureRecord pCurFailure = NULL;  CU_pFailureRecord pNextFailure = NULL;  pCurFailure = *ppFailure;  while (pCurFailure) {    if (pCurFailure->strCondition)      CU_FREE(pCurFailure->strCondition);    if (pCurFailure->strFileName)      CU_FREE(pCurFailure->strFileName);    pNextFailure = pCurFailure->pNext;    CU_FREE(pCurFailure);    pCurFailure = pNextFailure;  }  *ppFailure = NULL;}/*------------------------------------------------------------------------*//** Run all tests in a specified suite. * Internal function to run all tests in a suite.  The suite * need not be registered in the test registry to be run. * If the CUnit system is in an error condition after running * a test, no additional tests are run. * @param pSuite The suite containing the test (non-NULL). * @param pRunSummary The CU_RunSummary to receive the results (non-NULL). * @return A CU_ErrorCode indicating the status of the run. * @see CU_run_suite() for public interface function. * @see CU_run_all_tests() for running all suites. */static CU_ErrorCode run_single_suite(CU_pSuite pSuite, CU_pRunSummary pRunSummary){  CU_pTest pTest = NULL;  CU_ErrorCode result;  CU_ErrorCode result2;  assert(pSuite);  assert(pRunSummary);  f_pCurTest = NULL;  f_pCurSuite = pSuite;  CU_set_error(result = CUE_SUCCESS);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -