C: Unit test of static functions (module local)
When you want to unit test the functions you do not export, i.e., you have defined them with the “static” keyword, there are three ways to accomplish this:
- Add the unit tests at the end of the file containing the static functions.
- Expose the function declarations in the header (probably guarded by some
#ifdef UNIT_IMPL), and instead of writingstatic, use a preprocessor definition like VISIBILITY, which you redefine to an empty string if you want to build your unit test suite. Include the header in your separate unit test file. - Include the file under test in the unit test file:
// content of module_test.c: #include "module.c" // ...
I find the 3rd way to be the most elegant.
This post is licensed under CC BY-ND 4.0 by the author.