Reputation: 2195
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?
Upvotes: 0
Views: 826
Reputation: 366
Ran into the same issue with this. Indeed, CU_get_error()
will be 0
even if a testcase if failing. The following variables store the results as shown in the 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)
So a simple approach for checking if there has been any error, would be something like:
if (CU_get_number_of_tests_failed() != 0){
// Do Something
}
Upvotes: 1
Reputation: 2195
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
Upvotes: 2