CPPUNIT: common base test

238 views Asked by At

I would like to know an efficient design or implementation for testing base classes.
Given the example:

class Polygon;
class Rectangle : public Polygon;
class Triangle : public Polygon;

Where Rectangle and Triangle inherit from Polygon.

I would like to have the test classes for Rectangle and Triangle use a base testing class:

class Test_Rectangle : public CppUnit::TestFixture;
class Test_Triangle : public CppUnit::TestFixture;

But I don't know if the class Test_Polygon should inherit from CppUnit::TestFixture.

My present plan is: class Test_Polygon; class Test_Rectangle : public Test_Polygon, public CppUnit::TestFixture; class Test_Triangle : public Test_Polygon, public CppUnit::TestFixture;

The issue with the above plan is that Test_Polygon can't use CPPUNIT_ASSERT nor will its methods be listed in the CPPUNIT registry.

So what is the recommended hierarchy for testing base classes and descendants using CPPUNIT?

(I'm using CPPUNIT 1.12 with Visual Studio 2008 on Windows Vista.)

1

There are 1 answers

0
John Deters On BEST ANSWER

I think the answer depends on whether or not Polygon is a virtual base class. If it's virtual, then I would treat it as a "private" class and I wouldn't directly test it, and wouldn't need a Test_Polygon at all. Unit testing each of the subclasses should provide all the information of whether it functions properly or not.

If you are testing a method that's implemented in the base class, you could test it from any class that doesn't override it. If it's a method that's called from within your subclass, like Rectangle::SetSideLen(int len) { return Polygon::SetSideLen(len); }; then the testing should be accomplished by testing testRectangle.SetSideLen() in Test_Rectangle. And if you have no classes that don't override it, your base class is essentially unreachable code that probably should be removed.

If you can actually instantiate a stand-alone Polygon, then I would treat it like it's "public", so I would have Test_Polygon inherit from TestFixture in order to test those public methods.