Templates 带有模板化测试类的CppUnitTestingFramework
首先,这里描述了一个具有类似目标的问题: 这个问题是关于我试图解决同样的问题 使用Microsoft CPPFUnitTestFramework,我们可以使用以下内容创建单元测试:Templates 带有模板化测试类的CppUnitTestingFramework,templates,visual-c++,microsoft-cpp-unit-test,Templates,Visual C++,Microsoft Cpp Unit Test,首先,这里描述了一个具有类似目标的问题: 这个问题是关于我试图解决同样的问题 使用Microsoft CPPFUnitTestFramework,我们可以使用以下内容创建单元测试: using namespace Microsoft::VisualStudio::CppUnitTestFramework; namespace MyUnitTests { TEST_CLASS(NameOfMyTestClass) { public: TEST_METHOD(MyM
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
namespace MyUnitTests {
TEST_CLASS(NameOfMyTestClass) {
public:
TEST_METHOD(MyMethod1) {
Assert::IsTrue(false);
}
};
}
namespace MyUnitTests {
ONLY_USED_AT_NAMESPACE_SCOPE
template<MyEnumClass MeasurementType, char ExpectedShift>
class templatedScaleTestClass : public TestClass<templatedScaleTestClass<MeasurementType,ExpectedShift>>
{
public:
TEST_METHOD(Test_ExpectedShift) {
Assert::AreEqual(ExpectedShift, Calculations::getShiftAmount(MeasurementType));
}
};
ONLY_USED_AT_NAMESPACE_SCOPE template class templatedScaleTestClass<MyEnumClass::FIRST,3>;
ONLY_USED_AT_NAMESPACE_SCOPE template class templatedScaleTestClass<MyEnumClass::THIRD,1>;
}
template<typename T>
void debugMethod(TestClass<T> *tc) {
const TestClassInfo* classInfo = tc->__GetTestClassInfo();
std::stringstream msg;
msg << "Tag: " << classInfo->metadata->tag << std::endl;
msg << "helpMethodName: " << classInfo->metadata->helpMethodName << std::endl;
msg << "helpMethodDecoratedName: " << classInfo->metadata->helpMethodDecoratedName << std::endl;
msg << "New method address: " << &(classInfo->pNewMethod) << std::endl;
const MemberMethodInfo* methodInfo = T::__GetTestMethodInfo_Debug();
msg << "methodInfo - Tag: " << methodInfo->metadata->tag << std::endl;
msg << "methodInfo - methodName: " << methodInfo->metadata->methodName << std::endl;
msg << "methodInfo - helpMethodName: " << methodInfo->metadata->helpMethodName << std::endl;
msg << "methodInfo - helpMethodDecoratedName: " << methodInfo->metadata->helpMethodDecoratedName << std::endl;
msg << "methodInfo - lineNo: " << methodInfo->metadata->lineNo << std::endl;
Logger::WriteMessage(msg.str().c_str());
}
... (namespace, test class etc)
TEST_METHOD(Debug) { debugMethod(this); }
我想测试一组类似的测试(不使用For循环将所有断言放在一个test\u方法
),因此我查看了test\u类
宏:
#define TEST_CLASS(className) \
ONLY_USED_AT_NAMESPACE_SCOPE class className : public ::Microsoft::VisualStudio::CppUnitTestFramework::TestClass<className>
这会编译,在我看来,它应该允许我在template类中定义TEST\u METHOD
s的集合,然后实例化必要的枚举和常量值集合来设置它们(也许将来会对其他参数使用某种构造函数,尽管查看CppUnitTest.h会让我怀疑这是否是另一个问题……)
但是,该类从未出现在测试资源管理器中,尝试右键单击测试(在模板代码中)并单击“运行测试”将生成以下输出:
[datetime Informational] Executing test method 'MyUnitTests.templatedScaleTestClass<MeasurementType, ExpectedShift>.Test_ExpectedShift'
[datetime Informational] No tests found to run.
[datetime Information]正在执行测试方法“MyUnitTests.templatedScaleTestClass.test\u ExpectedShift”
[日期时间信息]未找到要运行的测试。
编辑:不确定最后一部分(“未找到要运行的测试”)的相关性-对正常测试(无用户端模板)执行相同操作生成相同的输出。单击某个特定测试会运行.cpp文件中的所有测试。也许我使用右键单击菜单时出错了。尽管我尝试了多次尝试将其显示出来,并检查了如下函数的输出:
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
namespace MyUnitTests {
TEST_CLASS(NameOfMyTestClass) {
public:
TEST_METHOD(MyMethod1) {
Assert::IsTrue(false);
}
};
}
namespace MyUnitTests {
ONLY_USED_AT_NAMESPACE_SCOPE
template<MyEnumClass MeasurementType, char ExpectedShift>
class templatedScaleTestClass : public TestClass<templatedScaleTestClass<MeasurementType,ExpectedShift>>
{
public:
TEST_METHOD(Test_ExpectedShift) {
Assert::AreEqual(ExpectedShift, Calculations::getShiftAmount(MeasurementType));
}
};
ONLY_USED_AT_NAMESPACE_SCOPE template class templatedScaleTestClass<MyEnumClass::FIRST,3>;
ONLY_USED_AT_NAMESPACE_SCOPE template class templatedScaleTestClass<MyEnumClass::THIRD,1>;
}
template<typename T>
void debugMethod(TestClass<T> *tc) {
const TestClassInfo* classInfo = tc->__GetTestClassInfo();
std::stringstream msg;
msg << "Tag: " << classInfo->metadata->tag << std::endl;
msg << "helpMethodName: " << classInfo->metadata->helpMethodName << std::endl;
msg << "helpMethodDecoratedName: " << classInfo->metadata->helpMethodDecoratedName << std::endl;
msg << "New method address: " << &(classInfo->pNewMethod) << std::endl;
const MemberMethodInfo* methodInfo = T::__GetTestMethodInfo_Debug();
msg << "methodInfo - Tag: " << methodInfo->metadata->tag << std::endl;
msg << "methodInfo - methodName: " << methodInfo->metadata->methodName << std::endl;
msg << "methodInfo - helpMethodName: " << methodInfo->metadata->helpMethodName << std::endl;
msg << "methodInfo - helpMethodDecoratedName: " << methodInfo->metadata->helpMethodDecoratedName << std::endl;
msg << "methodInfo - lineNo: " << methodInfo->metadata->lineNo << std::endl;
Logger::WriteMessage(msg.str().c_str());
}
... (namespace, test class etc)
TEST_METHOD(Debug) { debugMethod(this); }