Display content of an ASSERTed variable

772 Views Asked by At

Simple question here : is it possible to show the content of a variable freshly tested by a CUnit test? Printf() is not displayed during tests, and anyway I don't want to use it. For example, I would like CUnit to output the result of

(MQUEUE_PRI_MASK & flag)

Is it possible? Below is the current output of CUnit:

Suite: Message Queue Test
  Test: mqueue_init ...passed
  Test: mqueue : Test send & rcv functions ...passed
  Test: mqueue : Test mqueue priority ...passed
  Test: mqueue : Test mqueue flags ...0FAILED
    1. utils/test_mqueue.c:117  - CU_ASSERT_EQUAL((MQUEUE_PRI_MASK & flag ),MQUEUE_PRI_HIGH)
  Test: mqueue_free ...passed

Expected result :

Suite: Message Queue Test
  Test: mqueue_init ...passed
  Test: mqueue : Test send & rcv functions ...passed
  Test: mqueue : Test mqueue priority ...passed
  Test: mqueue : Test mqueue flags ...0FAILED
    1. utils/test_mqueue.c:117  - CU_ASSERT_EQUAL(5 ,MQUEUE_PRI_HIGH)
  Test: mqueue_free ...passed

Thanks!

2

There are 2 best solutions below

0
On BEST ANSWER

It's not possible with the built-in assert macros but it's not very difficult to write your own. Have a look at the macro definitions in <CUnit/CUnit.h>. You will probably have to write your own variant of CU_assertImplementation(). Use the original source as a template.

0
On

To add color to the last answer from @lars-ljung, here is an example of a replacement for CU_ASSERT_EQUAL() that tests integer values, called CU_ASSERT_INT_EQUAL(). Put this into <CUnit/CUnit.h>, and use this instead of the standard CU_ASSERT_EQUAL() when testing integer values.

You would need to write similar macros for long integers, doubles, strings, etc. etc.

Put this at the top of CUnit/CUnit.h:

#define CU_BUFFER_SIZE 1024

Put this in CUnit/CUnit.h maybe right next to the existing CU_ASSERT_EQUAL definition:

#define CU_ASSERT_INT_EQUAL(actual, expected) \
  { char buffer[CU_BUFFER_SIZE]; \
    snprintf(buffer, CU_BUFFER_SIZE, "CU_ASSERT_INT_EQUAL(%s (%d), %s (%d))", ""#actual"", actual, ""#expected"", expected); \
    CU_assertImplementation(((actual) == (expected)), __LINE__, buffer, __FILE__, CU_FUNC, CU_FALSE); }

This will produce a message like this in the output:

Test case

The assert in the test case is used just like you normally would use it:

CU_ASSERT_INT_EQUAL(function_error, SUCCESS);

Output

Suite: foo
  Test: test_my_failing_test ...FAILED
    1. :..\..\test\foo\test_foo.c:122  - CU_ASSERT_INT_EQUAL(function_error (4), SUCCESS (0))
  Test: test_my_passing_test ...passed