【问题标题】:C++ unit test testing, using template test classC++单元测试测试,使用模板测试类
【发布时间】:2013-06-30 18:29:21
【问题描述】:

我正在做一些 C++ 测试驱动开发。我有一组做同样事情的类,例如

相同的输入给出相同的输出(或者应该,这就是我要测试的)。我正在使用 Visual Studio 2012 的

CppUnitTestFramework。我想创建一个模板化的测试类,所以我编写了一次测试,并且可以根据需要在类中进行模板化,但是我找不到这样做的方法。我的目标:

/* 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 来做到这一点?

是否有另一个单元测试框架可以让我这样做?

【问题讨论】:

    标签: c++ unit-testing templates visual-c++ microsoft-cpp-unit-test


    【解决方案1】:

    我不知道有没有办法用 CppUnitTestFramework 做到这一点, 我不熟悉,但你当然可以 在googletest做 指定任意的类列表并具有框架 为所有这些生成(模板)相同的测试。我觉得 符合您的要求。

    您可以下载googletest作为源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
    }
    

    研究primersamples。然后去 AdvancedGuideTyped Tests

    还可以查看More Assertions

    【讨论】:

    【解决方案2】:

    我有一个类似的问题:我有一个接口和它的几个实现。当然,我只想针对接口编写测试。另外,我不想为每个实现复制我的测试。

    嗯,我的解决方案不是很漂亮,但它很简单,也是我迄今为止唯一想到的一个。

    您可以对 Class1 和 Class2 执行相同的操作,然后为每个实现添加更多专门的测试。

    setup.cpp

    #include "stdafx.h"
    
    class VehicleInterface
    {
    public:
        VehicleInterface();
        virtual ~VehicleInterface();
        virtual bool SetSpeed(int x) = 0;
    };
    
    class Car : public VehicleInterface {
    public:
        virtual bool SetSpeed(int x) {
            return(true);
        }
    };
    
    class Bike : public VehicleInterface {
    public:
        virtual bool SetSpeed(int x) {
            return(true);
        }
    };
    
    
    #define CLASS_UNDER_TEST Car
    #include "unittest.cpp"
    #undef CLASS_UNDER_TEST
    
    
    #define CLASS_UNDER_TEST Bike
    #include "unittest.cpp"
    #undef CLASS_UNDER_TEST
    

    unittest.cpp

    #include "stdafx.h"
    #include "CppUnitTest.h"
    
    #define CONCAT2(a, b) a ## b
    #define CONCAT(a, b) CONCAT2(a, b)
    
    using namespace Microsoft::VisualStudio::CppUnitTestFramework;
    
    
    TEST_CLASS(CONCAT(CLASS_UNDER_TEST, Test))
    {
    public:
        CLASS_UNDER_TEST vehicle;
        TEST_METHOD(CONCAT(CLASS_UNDER_TEST, _SpeedTest))
        {
            Assert::IsTrue(vehicle.SetSpeed(42));
        }
    };
    

    您需要从构建中排除“unittest.cpp”。

    【讨论】:

      猜你喜欢
      • 2010-09-28
      • 1970-01-01
      • 2013-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多