Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

Writing Tests for Interface Classes

Help
Enki
2010-08-12
2013-04-22
  • Enki
    Enki
    2010-08-12

    Hi,

    is there way to write tests for a bunch of classes by using their common interface class instead?

    I want to write a test only once using the interface defined by the root parent but I also want this particular test to run for every instance of the derived classes (for example by providing the instances through  factorys).

    How could I accomplish this in an elegant way?

     
  • Enki
    Enki
    2010-08-12

    Hi there! I found a solution myself, but it is not a particularly prety one. Here is an example (suppose IVehicle is the interface class to Car and Truck).

    #include <cppunit/config/SourcePrefix.h>
    #include <cppunit/extensions/HelperMacros.h>
    #include <ivehicle.h>
    #include <car.h>
    #include <truck.h>
    template <class T>
    class IVehicleTest : public CPPUNIT_NS::TestFixture
    {
        CPPUNIT_TEST_SUITE(IVehicleTest<T>);
        CPPUNIT_TEST(someTest);
        CPPUNIT_TEST_SUITE_END();
    private:
        IVehicle *pVehicle;
    public:
        void setUp()
        {
            pVehicle = new T();
        }
        void tearDown()
        {
            delete pVehicle;
        }
    protected:
        void someTest()
        {
            CPPUNIT_ASSERT( pVehicle->wrumwrum() );
        }
    };
    CPPUNIT_TEST_SUITE_REGISTRATION(IVehicleTest<Car>);
    CPPUNIT_TEST_SUITE_REGISTRATION(IVehicleTest<Truck>);
    
     
  • Enki
    Enki
    2010-08-12

    If you have got any better Ideas please tell me! Since the test output of the above approach does not work correctly.