How to use inheritance for a class with TEST_CLASS in CppUnitTestFramework

I have a class that inherits from another class:

class TestClass : public BaseClass 

I am wondering if this can be done with a test class using the TEST_CLASS macro or another macro that is part of the Microsoft Unit Testing Framework for C ++. I tried:

 class TEST_CLASS(TestClass : public BaseClass) 

But the IDE gives the error "Error: either a definition is expected or the tag name", and the compiler error is error C3861: "__GetTestClassInfo": the identifier was not found

I know that probably bad practice is inherited on the test class, but that will make it easier to implement the test. I am relatively new to C ++, so I wonder if this is something simple, I missed it or just can't.

Thanks,

+6
source share
2 answers

There is another option that you have not included, while others may stumble on this issue without knowing the solution.

In fact, you can get any arbitrary type by looking at the macro itself:

 /////////////////////////////////////////////////////////////////////////////////////////// // Macro to define your test class. // Note that you can only define your test class at namespace scope, // otherwise the compiler will raise an error. #define TEST_CLASS(className) \ ONLY_USED_AT_NAMESPACE_SCOPE class className : public ::Microsoft::VisualStudio::CppUnitTestFramework::TestClass<className> 

Since C ++ supports multiple inheritance, you can easily get code similar to the following:

 class ParentClass { public: ParentClass(); virtual ~ParentClass(); }; TEST_CLASS(MyTestClass), public ParentClass { }; 

Just remember that if you work with resources, you will need to have a virtual destructor for it to be called. You will also need to directly call the initialization and cleanup methods if you intend to use them, because the static methods that they create are not called automatically.

Good luck, good testing!

+7
source

Some time has passed since I used CppUnitTestFramework, but then this site was a valuable resource for many questions on this topic.

TEST_CLASS preprocessor macro . You can use it to declare a test class, for example

 TEST_CLASS(className) { TEST_METHOD(methodName) { // test method body } // and so on } 

What is it. As far as I know, there is no way to inherit test classes from each other.

Perhaps, although composition over inheritance may help in your particular case.

0
source

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


All Articles