I had a similar problem: I have an interface and several implementations of it. Of course, I only want to write tests against the interface. In addition, I do not want to copy my tests for each implementation.
Well, my solution is not very pretty, but it's just and the only thing that I have come up with so far.
You can do the same for Class1 and Class2, and then add more specialized tests for each implementation.
setup.cpp
#include "stdafx.h" class VehicleInterface { public: VehicleInterface(); virtual ~VehicleInterface(); virtual bool SetSpeed(int x) = 0; }; class Car : public VehicleInterface { public: virtual bool SetSpeed(int x) { return(true); } }; class Bike : public VehicleInterface { public: virtual bool SetSpeed(int x) { return(true); } }; #define CLASS_UNDER_TEST Car #include "unittest.cpp" #undef CLASS_UNDER_TEST #define CLASS_UNDER_TEST Bike #include "unittest.cpp" #undef CLASS_UNDER_TEST
unittest.cpp
#include "stdafx.h" #include "CppUnitTest.h" #define CONCAT2(a, b) a ## b #define CONCAT(a, b) CONCAT2(a, b) using namespace Microsoft::VisualStudio::CppUnitTestFramework; TEST_CLASS(CONCAT(CLASS_UNDER_TEST, Test)) { public: CLASS_UNDER_TEST vehicle; TEST_METHOD(CONCAT(CLASS_UNDER_TEST, _SpeedTest)) { Assert::IsTrue(vehicle.SetSpeed(42)); } };
You will need to exclude "unittest.cpp" from the assembly.
source share