2

I need to test several functions that are defined as static in a file(not used anywhere outside this file except in the unit test).

To enable the unit test file to see the functions I tried using an .h file in which these functions are declared, however this results in a linkage error. A solution I considered is, instead of using the static keyword, using a macro that is replace by static in the working version an is empty in the unit test version.

However I don't know how to make the condition dependent on the startup project instead of manually redefining macro all the time.Seeing as the code is intended to run on a TI processor I also considered using a wrapper function that is only compiled under when WIN32 is defined.

I would very much like to hear feedback on these ideas and better ideas. Thanks!

KennyPowers
  • 4,925
  • 8
  • 36
  • 51

1 Answers1

4

In your source file you need

#ifndef UNITTESTS
    #define STATIC
#else
    #define STATIC static
#endif

Then when you build your unit tests pass-DUNITTESTS to make

Otherwise consider including your source file in your tests, i.e.

#include "file.c"
jayant
  • 2,349
  • 1
  • 19
  • 27
  • I'm no fan of the macro solution because it requires modifying your code. Both by defining an extra macro, and testing a code that is (slightly) different from the real one. Think of a possible compiler bug on the interpretation of the static keyword – PPC Mar 20 '18 at 13:54