What are common/widely accepted practices for unit testing class hierarchies?
I've tried to look for different questions that are related to this problem, but couldn't find comprehensive one.
Let's suppose there is a hierarchy under test.
class Base
{
int base_internal() { return 10;}
public:
int virtual do_f() { return 0; }
int execute() {
return do_f()*base_internal();
}
};
class DerrivedA : public Base
{
int a_specefic() { return 4; }
public:
int virtual do_f() { return a_specefic(); }
};
class DerrivedB : public Base
{
int b_specefic() { return 8; }
public:
int virtual do_f() { return b_specefic(); }
};
Now for unit testing infrastructure I am thinking about two strategies here.
Strategy 1. Have "tester" class for each type, derive from appropriate class and do the testing
class TestForBase : public Base {...};
class TestForDerrivedA : public DerrivedA {...};
class TestForDerrivedB : public DerrivedB {....};
Strategy 2. Use "concertized" version of visitor design pattern, (which will have single TestVisitor), than modify each class under test with accept() method, and do the testing inside visit() of Tester class
class Base
{
void accept(Tester& t);
int base_internal();
public:
int virtual do_f();
int execute();
};
class Derrived : public Base
{
void accept(Tester& t);
int a_specefic();
public:
int virtual do_f();
};
struct Tester
{
void visit(Base* b) { ... };
void visit(Derrived* d) { ... } ;
};
What are the other options?