C ++ unit test testing using test class template

I am doing some C ++ development. I have a set of classes that do the same thing, for example.

the same input gives the same result (or should, what I'm trying to verify). I am using Visual Studio 2012s

CppUnitTestFramework. I wanted to create a template test class, so I write tests once and can use the template in classes as needed, but I can’t find a way to do this. My goal:

/* two classes that do the same thing */ class Class1 { int method() { return 1; } }; class Class2 { int method() { return 1; } }; /* one set of tests for all classes */ template< class T> TEST_CLASS(BaseTestClass) { TEST_METHOD(testMethod) { T obj; Assert::AreEqual( 1, obj.method()); } }; /* only have to write small amout to test new class */ class TestClass1 : BaseTestClass<Class1> { }; class TestClass2 : BaseTestClass<Class1> { }; 

Is there a way to do this using CppUnitTestFramework?

Is there another unit testing system that would allow me to do this?

+4
source share
2 answers

I don’t know if there is a way to do this using CppUnitTestFramework, with which I am unfamiliar, but something that you can certainly do googletest gives an arbitrary list of classes and has the structure to generate (by template) the same test for all of them. I think it will match your score.

You can download googletest as a source here .

The idiom you want is:

 typedef ::testing::Types</* List of types to test */> MyTypes; ... TYPED_TEST_CASE(FooTest, MyTypes); ... TYPED_TEST(FooTest, DoesBlah) { /* Here TypeParam is instantiated for each of the types in MyTypes. If there are N types you get N tests. */ // ...test code } TYPED_TEST(FooTest, DoesSomethingElse) { // ...test code } 

Learn primer and samples . Then go to AdvancedGuide for Typed Tests

Also check Additional statements

+2
source

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.

0
source

Source: https://habr.com/ru/post/1489420/


All Articles