How to unit test static callback routine called from ISR

644 views Asked by At

I am using ceedling to unit test a C application and trying to achieve high coverage ~100%.

I have a static callback function in one of my application module, which is registered to SDK function with function pointer and is called at certain event from the SDK.

In appModule.c,

typedef void( *type_appCallback ) ( void );

static void appCallback( void );

I want to unit test this function and since this functions is static will not be seen in the ceedling test_appModule.c. I have a work around to this define TEST_STATIC instead of static,

#ifdef TEST 
TEST_STATIC
#else
TEST_STATIC static
#endif

But I am not big fan of this work around, any suggestions to above problem?

2

There are 2 answers

0
Bodo On

As a workaround you could use a wrapper module that includes the C file.

wrap_app_Module.c

/* include code of static function (.c is intentional, not .h) */
#include "appModule.c"

void wrap_appCallback(void)
{
    appCallback();
}

Instead of the static function you can call the wrapper function in your tests.

0
kayzej1141 On

I did something similar to what @Bodo suggested above using the CMock callback plugin. Note the cmockNumCalls is a mandatory parameter for the stub function

Production code -> setCallback( funcAddress );

Unit Test code -> setCallback_Stub( storeFuncAddress );

Where storeFuncAddress looks like

void storeFuncAddress(
      type_appCallback       appCallbackFn,
      int                    cmockNumCalls)
{
    l_storedCallbackFn = appCallbackFn;
}

When the _Stub version gets called it passes the same parameters to the registered callback function so storeFuncAddress receives the same function address (which is a pointer to a static) and stores it in a variable of type type_appCallback which I'm calling l_storedCallbackFn.

The second part is where the event gets triggered.

Production code -> triggerCallback();

Unit Test code -> triggerCallback_Stub( runStoredCallbackFunction );

Where my runStoredCallbackFunction function looks like

void runStoredCallbackFunction(
    int                       cmockNumCalls)
{
    (*l_storedCallbackFn)(NULL);
}

Hopefully this helps you or the next person who runs across this same question.