Search code examples
testingassertcunit

CUnit return 0 on test fail


I built a simple program that executes a test in CUnit. The main function is:

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 }

The test_parse function uses CU_ASSERT_FATAL. The test fails, but the output of main is the following:

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

The main() returns 0. It returns 0 also if the test passes. What am I doing wrong?


Solution

  • My error: CU_get_error() returns an error code only if a framework function had an error, not the tests. To get test results, follow http://cunit.sourceforge.net/doc/running_tests.html