I am using cmocka to do some unit testing on my C-Project and I am wondering how to handle static emelents.
Static elements are for me:
- Functions declared as static
- Variables inside functions which are declared as static
So let the function fut
be our function under test, and foo
be an other function. Both placed in the file bar.c
:
static int fut(int add) {
static int sum = 0;
sum += add;
return sum;
}
int foo(int someVar){
//Some calculation on someVar...
someVar = someVar * 42;
//call subRoutine
return fut(someVar);
}
And let foo.h look like this:
extern int foo(int someVar);
So lets go on I'll show the problem. I would like to test the function under test by two undepended tests which pass some random values for add
. The testroutines are placed in main.c and are looking like this:
void fut_test_1(void **state) {
int ret;
ret = fut(15);
assert_int_equal(ret, 15);
ret = fut(21);
assert_int_equal(ret, 36);
}
void fut_test_2(void **state) {
int ret;
ret = fut(32);
assert_int_equal(ret, 32);
ret = fut(17);
assert_int_equal(ret, 49);
}
Now I could try to compile the unit test with something like: gcc main.c foo.c -Icmocka
There are now two problems:
The function declared as static cannot be accessed from
main.c
, so the linker will stop during the build process.The variable inside the function which are declared as static will not be reset between the two tests. So the
fut_test_2
will fail.
How to handle these problems with the mentioned static elements?