Short answer: Do not do this.
Your test should only test on the public interface. Let me try to explain with code:
class Adder { int a,b; public: Adder() : a(0),b(0) {} void set(int x,int y) { a=x;b=y; } int get() { return a+b; } };
and a test (suppose we had access to a and b at some point):
void testAdder(){ Adder add; int a = 1; int b = 2; add.set(a,b); ASSERT_EQUALS(add.a,a); ASSERT_EQUALS(add.b,b); ASSERT_EQUALS(add.get(),a+b); }
Suppose you already distributed the code, and someone uses it. He would like to continue using it, but complains about too much memory consumption. To fix this problem is simple, keeping the same public interface:
class Adder { int c; public: Adder() : c(0) {} void set(int x,int y) { c = x+y; } int get() { return c; } };
It was easy, but the test failed :(
Conclusion Testing the details of a private implementation hits the target of testing, because every time you change the code, it is likely that you also need to โfixโ the test.
user463035818
source share