Kevin Yu Kevin Yu - 1 month ago 7
C Question

How to test a static function

As applying unit-test to some C code, we run into a problem that some static function can not be called at the test file, without modifying the source code. Is there any simple or reasonable way to overcome this problem?

Answer

I have a test harness. In dire cases - like trying to test a static function, I use:

#include "code_under_test.c"
...test framework...

That is, I include the whole of the file containing the function under test in the test harness. It is a last resort - but it works.

Comments