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:
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?
Based on @LPs comment and my own ideas I would like to conclude possible solutions:
Regarding the Problem one with Functions declared as static:
bar.c
into the test driving main.c
by #include "foo.c"
.The tests fut_test_2
and fut_test_2
could by placed into bar.c
which contains then both: The function under test fut
and the tests. The tests can then be made accessible by adding the declaration to foo.h
:
extern int foo(int someVar);
extern void fut_test_1(void **state);
extern void fut_test_2(void **state);
Regarding the Problem with the static variables: