Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 702
Armen Tsirunyan Avatar asked Feb 26 '23 02:02

Armen Tsirunyan


1 Answers

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.

like image 116
Lie Ryan Avatar answered Mar 07 '23 12:03

Lie Ryan