用CppUnitTestFramework继承

时间:2019-06-11 12:58:08

标签: c++ unit-testing cppunit microsoft-cpp-unit-test

我想创建全局类,因为我想在测试中进行相同的初始化。我尝试过那样,我消除了诸如歧义访问之类的错误。有人有主意吗?

#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'

1 个答案:

答案 0 :(得分:1)

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");
    }
};