Reputation: 113
I am trying to write a test case in c using cmocka library.My testcase is testing a function which then internally calls a function from a 3rd party library (cannot modify the library).This function return NULL value when application is not up and running,so i want to mock the return value for this 3rd party library function.How can i achieve this?
I have tried using will_return function of cmocka to get desired return value,but it does not work
void third_party_func()
{
return mock();
}
void my_func_to_be_tested()
{
int ret;
ret = third_party_func();
return ret;
}
void test_do_mytest(void ** state)
{
(void) state;
int ret;
will_return(third_party_func,1);
ret = my_func_to_be_tested();
assert_int_equal(1,ret);
}
const struct CMUnitTest tests[] = {
cmocka_unit_test(test_do_mytest),
};
int main(void)
{
return cmocka_run_group_tests(tests, NULL, NULL);
}
I get compilation error that multiple definition for third_party_func().How to handle such case?
I want to get desired value as return value for my third party func.
Upvotes: 3
Views: 1651
Reputation: 101
Have you tried the __wrap_ flag ?
Change the name of your function from third_party_func to __wrap_third_party_func and add the following directive to gcc , for example with fopen function :
FILE * __wrap_fopen(const char *__restrict __filename,
const char *__restrict __modes)
{
return mock_ptr_type(FILE*);
}
and add the directive to gcc like :
$ gcc -g -Wl,--wrap=fopen
Hope it'll help !
Upvotes: 2