CUnit在测试失败时返回0

时间:2018-11-06 15:26:48

标签: testing assert cunit

我构建了一个简单的程序,该程序在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。我在做什么错了?

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

与此同时遇到了同样的问题。实际上,即使测试用例失败,CU_get_error()也将是0。以下变量存储结果,如doc

中所示
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
}