While developing unit tests, accessing private members to check the internal state of the class can be necessary. Sometimes getter and setter functions are not available, sometimes they are not public.
First method to handle that, writing a preprocessor define writes publis instead of private and protected.
#define protected public
#define private public
Second method is making the test classes friends of the classes.
class test_foo {
};
class foo {
private:
int mem;
friend class test_foo;
};
A third method is creating public interface for test.
class foo {
#if defined FOO_UNIT_TEST
public:
int get_mem() const;
#endif
private:
int mem;
};
Is there any other method except these methods? Every method has pros and cons. Which one can be thought as best practice?
Thanks.
Aucun commentaire:
Enregistrer un commentaire