cunit-testingembeddedcmockunity-test-framework

How to mock function in same UUT in C


I am learning unit testing using Ceedling, with CMock for mocks, on an existing, embedded C code-base (which I can adjust to suit).

I have come across the situation where one function in a unit calls a second function within the same unit.

int foo_a(int r) 
{
    /* foo_a work */
    /* do not wish to test this function just to test foo_b. */
}

int foo_b(int i) /* function to test */
{
    /* foo_b work */
    if (some_condition)
        foo_a(k); /* need to test if foo_a() is called or not. */
}

I don't want to test the foo_a() as part of the foo_b() tests. How do I mock foo_a() so that I can still test if it is called or not but without testing the foo_a() function itself?


Solution

  • You can't do this without modifying your source code.

    Consider breaking the functions out into two separate compilation units.

    Or, add an #ifndef TEST around the function you want to replace.