0

我构建了一个在 CUnit 中执行测试的简单程序。主要功能是:

int main()
 50 {
 51         CU_pSuite pSuite = NULL;
 52 
 53         /* initialize the CUnit test registry */
 54         if (CUE_SUCCESS != CU_initialize_registry())
 55                 return CU_get_error();
 56 
 57         /* add a suite to the registry */
 58         pSuite = CU_add_suite("Suite_1", init_suite1, clean_suite1);
 59         if (NULL == pSuite) { 
 60                 CU_cleanup_registry();
 61                 return CU_get_error();
 62         } 
 63 
 64         if ((NULL == CU_add_test(pSuite, "test of fprintf()", test_parse))) { 
 65                 CU_cleanup_registry();
 66                 return CU_get_error();
 67         } 
 68 
 69         /* Run all tests using the CUnit Basic interface */
 70         CU_basic_set_mode(CU_BRM_VERBOSE);
 71         CU_basic_run_tests();
 72         CU_cleanup_registry();
 73         printf("ERROR CODE: %d", CU_get_error());
 74         return CU_get_error();
 75 }

test_parse 函数使用 CU_ASSERT_FATAL。测试失败,但 main 的输出如下:

CUnit - A unit testing framework for C - Version 2.1-3
http://cunit.sourceforge.net/


Suite: Suite_1
  Test: test of fprintf() ...FAILED
    1. /home/fedetask/Desktop/curl/tests/main.c:42  - parsed == 3

Run Summary:    Type  Total    Ran Passed Failed Inactive
              suites      1      1    n/a      0        0
               tests      1      1      0      1        0
             asserts      5      5      4      1      n/a

Elapsed time =    0.000 seconds
ERROR CODE: 0

main() 返回 0。如果测试通过,它也会返回 0。我究竟做错了什么?

4

2 回答 2

1

我的错误: CU_get_error() 仅在框架函数有错误而不是测试时才返回错误代码。要获得测试结果,请遵循http://cunit.sourceforge.net/doc/running_tests.html

于 2018-11-06T15:41:55.193 回答
0

遇到了同样的问题。事实上,即使测试用例失败,CU_get_error()也会如此。0以下变量存储结果,如文档中所示

unsigned int CU_get_number_of_suites_run(void)
unsigned int CU_get_number_of_suites_failed(void)
unsigned int CU_get_number_of_tests_run(void)
unsigned int CU_get_number_of_tests_failed(void)
unsigned int CU_get_number_of_asserts(void)
unsigned int CU_get_number_of_successes(void)
unsigned int CU_get_number_of_failures(void) 

因此,检查是否有任何错误的简单方法如下:

if (CU_get_number_of_tests_failed() != 0){
  // Do Something
}
于 2020-02-19T14:35:57.643 回答