Assuming I have the following (very simple) class
class Foo
{
public:
void setAB(int a, int b) { m_a = a; m_b = b; m_aIsBigger = a > b; }
int getA() const { return m_a; }
int getB() const { return m_b; }
bool isABigger() const { return m_aIsBigger; }
protected:
int m_a;
int m_b;
bool m_aIsBigger;
}
and I want to put it under automated test (e.g. unit test, module test). I can see two possible ways to test the functions:
Either I can use its own functions to test it, i.e. invoke setAB()
and then verify that getA()
, getB()
, and isABigger()
return the expected values. Or I could directly set m_a
(e.g. by subclassing Foo), then invoke getA()
and verify that the value is identical to the one I set.
The first method seems more "organic", as it uses the class' built-in functionality and is independent of its implementation (e.g. in the case I realize that I could calculate "a > b" on the fly for isABigger()
instead of using m_aIsBigger
). However it seems to violate the "test only one thing" principle for tests (even if I separate getA()
, getB()
, and isABigger()
in their own test cases, I cannot test them apart from setAB()
; nor can I test setAB()
without invoking getA()
and getB()
)
If I subclass Foo I can test each function individually, but it feels kinda wrong to manipulate the class' interna this way, and is far more brittle (changing isABigger()
to calculate the result on the fly would require a change in the test - even if it feels it should not).
So the question is: what is the better way to set up an object for testing - or is it merely a matter of personal preferrence?
private
. If something can't be reached from the public interface in a test, it can't be reached from the public interface in use.