问题描述
限时送ChatGPT账号..我正在做一些 C++ 测试驱动开发.我有一组类做同样的事情,例如
I’m doing some C++ test driven development. I have a set of classes the do the same thing e.g.
相同的输入给出相同的输出(或者应该,这就是我试图测试的).我使用的是 Visual Studio 2012 的
same input gives same output (or should, that’s what I’m try to test). I’m using Visual Studio 2012’s
CppUnitTestFramework.我想创建一个模板化的测试类,所以我编写了一次测试,并且可以根据需要在类中模板化,但是我找不到这样做的方法.我的目标:
CppUnitTestFramework. I wanted to create a templated test class, so I write the tests once, and can template in classes as needed however I cannot find a way to do this. My aim:
/* two classes that do the same thing */
class Class1
{
int method()
{
return 1;
}
};
class Class2
{
int method()
{
return 1;
}
};
/* one set of tests for all classes */
template< class T>
TEST_CLASS(BaseTestClass)
{
TEST_METHOD(testMethod)
{
T obj;
Assert::AreEqual( 1, obj.method());
}
};
/* only have to write small amout to test new class */
class TestClass1 : BaseTestClass<Class1>
{
};
class TestClass2 : BaseTestClass<Class1>
{
};
有没有办法使用 CppUnitTestFramework 来做到这一点?
Is there a way I can do this using CppUnitTestFramework?
是否有其他单元测试框架可以让我这样做?
Is there another unit testing framework that would allow me to do this?
推荐答案
我不知道 CppUnitTestFramework 有没有办法做到这一点,我不熟悉,但你肯定可以在 googletest 中进行指定一个任意的类列表并具有框架为所有这些生成(模板方式)相同的测试.我觉得符合您的要求.
I do not know if there is a way to do this with CppUnitTestFramework, with which I am unfamiliar, but something you can certainly do in googletest is specify an arbitrary list of classes and have the framework generate (template-wise) the same test(s) for all of them. I think that would fit your bill.
您可以在此处下载 googletest 作为源.
You can download googletest as source here.
你想要的成语是:
typedef ::testing::Types</* List of types to test */> MyTypes;
...
TYPED_TEST_CASE(FooTest, MyTypes);
...
TYPED_TEST(FooTest, DoesBlah) {
/* Here TypeParam is instantiated for each of the types
in MyTypes. If there are N types you get N tests.
*/
// ...test code
}
TYPED_TEST(FooTest, DoesSomethingElse) {
// ...test code
}
研究 primer 和示例.然后去高级指南对于类型化测试
Study the primer and the samples. Then go to the AdvancedGuide for Typed Tests
另请查看更多断言一个>
这篇关于C++单元测试测试,使用模板测试类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论