Correct way to temporarily enable and disable function wrappers in cmocka?

I am using the cmocka library to test the inline c code. According to the documentation, I use a prefix __wrap_to extract functions so that I can isolate my unit tests. However, as soon as I do this, all function calls will go to the wrapped function forever. How to re-enable a real function in certain circumstances so that I can test it or allow other functions to be used? It seems to me the only way is to use the global field as a switch to call a real function as follows:

int __wrap_my_function(void) {
    if (g_disable_wrap_my_function) {
        return __real_my_function();
    }

    // ... do mock stuff
}

Is this the right way to do this?

+4
source share
2 answers

In the end, I did what I suggested in my question. I used a global variable that I check in a wrapped function for eanble and disables the mockery at runtime.

0
source

You simply compile without the -wrap command line option.

Or you use defines:

#include <cmocka.h>
#ifdef UNIT_TESTING
#define strdup test_strdup
#endif

Add the dummy function test_strdup. Now you can use this function for testing.

+3
source

All Articles