Never mind, I got it ...
The solution that worked for me is:
1. Create the test base class, that derives from cppUnit::TestCase, and
define the tests for the protocol in it. Two things to notice: the
termination macro for the declaration of the tests in the fixture must
be CPPUNIT_TEST_SUITE_END_ABSTRACT() and the class should not be
registered as a fixture. See below (the auto_ptr is to avoid having to
deallocate the memory explicitly):
#include <memory>
using namespace std;
class TestSetterGetter: public CppUnit::TestCase {
CPPUNIT_TEST_SUITE( TestSetterGetter );
CPPUNIT_TEST( testSetGet );
// Different termination macro for the test declarations.
CPPUNIT_TEST_SUITE_END_ABSTRACT();
auto_ptr<SetterGetter> psetterGetter_;
virtual auto_ptr<SetterGetter> makeSetterGetter() = 0;
public:
void setUp() {psetterGetter_ = makeSetterGetter();}
void tearDown() {}
protected:
void testSetGet() {
psetterGetter_->set(1);
CPPUNIT_ASSERT_EQUAL(1, psetterGetter_->get());
}
};
// No test registration here.
2. Then, for the implementations of the tests of the different
implementations (sorry for the tongue twisting), one thing to notice is
that the initialization of the test declaration requires a different
macro which includes a declaration of the base test. See below:
class TestImpl1: public TestSetterGetter {
// A different macro for the initialization of the test
declarations.
CPPUNIT_TEST_SUB_SUITE( TestImpl1, TestSetterGetter );
CPPUNIT_TEST_SUITE_END();
auto_ptr<SetterGetter> makeSetterGetter() {
return auto_ptr<SetterGetter>(new Impl1());
}
public:
// In my example, no extra tests to add here
};
CPPUNIT_TEST_SUITE_REGISTRATION( TestImpl1 ); // Just register your
fixture and you will be in business.
class TestImpl2: public TestSetterGetter {
CPPUNIT_TEST_SUB_SUITE( TestImpl2, TestSetterGetter );
CPPUNIT_TEST_SUITE_END();
auto_ptr<SetterGetter> makeSetterGetter() {return
auto_ptr<SetterGetter>(new Impl2());}
public:
// In my example, no extra tests to add here
};
CPPUNIT_TEST_SUITE_REGISTRATION( TestImpl2 );
All of this complication only because reflection is nowhere to be seen
on C++.
|