2014-02-14 36 views
1

我正在嘗試使用單個Google測試來測試方法。但是,該方法被各種子類覆蓋。我如何確保Google Test將測試應用於覆蓋正在測試的測試的所有方法?例如:如何使用Google測試將統一測試應用於所有子類?

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

您可以使用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(); 
} 
相關問題