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

📄 testrun.c

📁 CUNIT c 测试框架,类似大名鼎鼎的junit,很有用的,这里是原代码,可以编译生成lib文件
💻 C
📖 第 1 页 / 共 5 页
字号:
 */
/*------------------------------------------------------------------------*/
/** Run all tests in all suites registered in the test registry.
 *  The suites are run in the order registered in the test registry.
 *  For each registered suite, any initialization function is first
 *  called, the suite is run using run_single_suite(), and finally
 *  any  suite cleanup function is called.  If an error condition
 *  (other than CUE_NOREGISTRY) occurs during the run, the action
 *  depends on the current error action (see CU_set_error_action()).
 *  @return A CU_ErrorCode indicating the first error condition
 *          encountered while running the tests.
 *  @see CU_run_suite() to run the tests in a specific suite.
 *  @see CU_run_test() for run a specific test only.
 */
CU_ErrorCode CU_run_all_tests(void)
{
  CU_pTestRegistry pRegistry = CU_get_registry();
  CU_pSuite pSuite = NULL;
  CU_ErrorCode result;
  CU_ErrorCode result2;

  CU_set_error(result = CUE_SUCCESS);
  if (NULL == pRegistry) {
    CU_set_error(result = CUE_NOREGISTRY);
  }
  else {
    /* test run is starting - set flag */
    f_bTestIsRunning = CU_TRUE;

    /* Clear results from the previous run */
    clear_previous_results(&f_run_summary, &f_failure_list);

    pSuite = pRegistry->pSuite;
    while ((NULL != pSuite) && ((CUE_SUCCESS == result) || (CU_get_error_action() == CUEA_IGNORE))) {
      /* if the suite has tests, run it */
      if (pSuite->uiNumberOfTests > 0) {
        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 = CU_FALSE;

    if (NULL != 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 (NULL == pSuite) {
    CU_set_error(result = CUE_NOSUITE);
  }
  else {
    /* test run is starting - set flag */
    f_bTestIsRunning = CU_TRUE;

    /* Clear results from the previous run */
    clear_previous_results(&f_run_summary, &f_failure_list);

    if (pSuite->uiNumberOfTests > 0) {
      result = run_single_suite(pSuite, &f_run_summary);
    }

    /* test run is complete - clear flag */
    f_bTestIsRunning = CU_FALSE;

    if (NULL != 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 (NULL == pSuite) {
    CU_set_error(result = CUE_NOSUITE);
  }
  else if (NULL == pTest) {
    CU_set_error(result = CUE_NOTEST);
  }
  else if ((NULL == pTest->pName) || (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 = CU_TRUE;

    /* Clear results from the previous run */
    clear_previous_results(&f_run_summary, &f_failure_list);

    f_pCurTest = NULL;
    f_pCurSuite = pSuite;

    if ((NULL != pSuite->pInitializeFunc) && (0 != (*pSuite->pInitializeFunc)())) {
      if (NULL != 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 = CU_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 ((NULL != pSuite->pCleanupFunc) && (0 != (*pSuite->pCleanupFunc)())) {
        if (NULL != f_pSuiteCleanupFailureMessageHandler) {
          (*f_pSuiteCleanupFailureMessageHandler)(pSuite);
        }
        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 = CU_FALSE;

      if (NULL != 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>CU_TRUE</CODE> if a test run is in progress,
 *  <CODE>CU_TRUE</CODE> otherwise.
 */
CU_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 it points to a NULL pointer, 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;

  assert(NULL != ppFailure);

  pFailureNew = (CU_pFailureRecord)CU_MALLOC(sizeof(CU_FailureRecord));

  if (NULL == pFailureNew) {
    return;
  }

  pFailureNew->strFileName = NULL;
  pFailureNew->strCondition = NULL;
  if (NULL != szFileName) {
    pFailureNew->strFileName = (char*)CU_MALLOC(strlen(szFileName) + 1);
    if(NULL == pFailureNew->strFileName) {
      CU_FREE(pFailureNew);
      return;
    }
    strcpy(pFailureNew->strFileName, szFileName);
  }

  if (NULL != szCondition) {
    pFailureNew->strCondition = (char*)CU_MALLOC(strlen(szCondition) + 1);
    if (NULL == pFailureNew->strCondition) {
      if(NULL != 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 (NULL != pTemp) {
    while (NULL != 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 (non-NULL).
 *  @param ppFailure   The failure record to clean (non-NULL).
 *  @see CU_clear_previous_results()
 */
static void clear_previous_results(CU_pRunSummary pRunSummary, CU_pFailureRecord* ppFailure)
{
  assert(NULL != pRunSummary);
  assert(NULL != 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;

⌨️ 快捷键说明

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