Search code examples
c++unit-testingc-preprocessorfriend

White-box testing - friends or preprocessor?


Imagine we have a class like this:

class Testee
{
public:
   void Func()
private:
   void auxFunc()
};

and we want to do white-box unit-testing on it. Which do you think is a better approach? To declare the tester class a friend of the testee class? Or use the preprocessor like this:

  class Testee
    {
    public:
       void Func()
#ifndef UNITTEST_SYMBOL
    private:
#elif
    public:
#endif
       void auxFunc()
    };

and later in the testing file

#define UNITTEST_SYMBOL
#include "Testee.h"
#undef UNITTEST_SYMBOL

So, again, which do you think is a better approach? Or maybe you could suggest another approach.


Solution

  • How about:

    #ifndef UNITTEST_SYMBOL
    #define semiprivate private 
    #else
    #define semiprivate public
    #endif
    

    and declare your class like:

      class Testee
        {
        public:
           void Func()
        semiprivate:
           void auxFunc()
        };
    

    or even, if you're daring enough, do #define private public when testing.