使用 CppUnitTestFramework 继承

Inheritance with CppUnitTestFramework

本文关键字:继承 CppUnitTestFramework 使用      更新时间:2023-10-16

我想创建全局类,因为我想在我的测试中进行相同的初始化。我尝试过,我多次出现诸如模棱两可的访问之类的错误。有人有想法吗?

#include <CppUnitTest.h>

using namespace Microsoft::VisualStudio::CppUnitTestFramework;

TEST_CLASS(GLOBAL_TEST)
{
public:
    TEST_METHOD_INITIALIZE(initialize)
    {
        Logger::WriteMessage("INITIALIZE");
    }
};
TEST_CLASS(ClassA), public GLOBAL_TEST
{
public:
    TEST_METHOD(ClassA_Test1)
    {
        Logger::WriteMessage("ClassA_Test1");
    }
};

我的错误 :

Code    Description
C2385   ambiguous access of '__GetTestClassInfo'
C2385   ambiguous access of '__GetTestVersion'  
C2594   'static_cast': ambiguous conversions from 'void (__cdecl     ClassA::ClassA::* )(void)' to 'Microsoft::VisualStudio::CppUnitTestFramework::TestClassImpl::__voidFunc'

TEST_宏不支持继承,但基类可以定义为包含Initialize()方法的普通类。不过,您仍然必须在每个派生类中定义一个TEST_METHOD_INITIALIZE函数。

#include <CppUnitTest.h>
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
class GLOBAL_TEST
{
public:
    void Initialize()
    {
        Logger::WriteMessage("INITIALIZE");
    }
};
TEST_CLASS(ClassA), public GLOBAL_TEST
{
public:
    TEST_METHOD_INITIALIZE(MethodInitialize)
    {
        Initialize();
    }
    TEST_METHOD(ClassA_Test1)
    {
        Logger::WriteMessage("ClassA_Test1");
    }
};