gpt4 book ai didi

c++ - 与 CppUnitTestFramework 的继承

转载 作者:行者123 更新时间:2023-12-03 12:47:00 35 4
gpt4 key购买 nike

我想创建全局类,因为我想在我的测试中进行相同的初始化。我这样尝试过,但出现了多个错误,例如访问不明确。有人有想法吗?

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

关于c++ - 与 CppUnitTestFramework 的继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56544309/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com