如何使用Google Test对所有子类应用单一测试?

时间:2014-02-14 22:03:19

标签: c++ testing googletest

我正在尝试使用单个Google测试来测试方法。但是,该方法被各种子类覆盖。如何确保Google Test将测试应用于覆盖我正在测试的所有方法?例如:

class Base {
    public:
        virtual void foo() = 0;
}

class Derived : public Base{
    public:
        void foo(){
           /*This is the code I want Google Test to test */
        }
}

class Derived2 : public Base{
    public:
        void foo(){
           /*This is the code I want Google Test to test */
        }
}

1 个答案:

答案 0 :(得分:1)

您可以使用typed teststype-parameterised tests来执行此操作。

以下是与您的示例匹配的类型化测试:

// A test fixture class template where 'T' will be a type you want to test
template <typename T>
class DerivedTest : public ::testing::Test {
 protected:
  DerivedTest() : derived_() {}
  T derived_;
};

// Create a list of types, each of which will be used as the test fixture's 'T'
typedef ::testing::Types<Derived, Derived2> DerivedTypes;
TYPED_TEST_CASE(DerivedTest, DerivedTypes);

// Create test cases in a similar way to the basic TEST_F macro.
TYPED_TEST(DerivedTest, DoFoo) {
  this->derived_.foo();
  // TypeParam is the type of the template parameter 'T' in the fixture
  TypeParam another_derived;
  another_derived.foo();
}