gpt4 book ai didi

c++ - 如何使用 Google Test 将单一测试应用于所有子类?

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:24:07 25 4
gpt4 key购买 nike

我正在尝试使用单个 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 */
}
}

最佳答案

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

关于c++ - 如何使用 Google Test 将单一测试应用于所有子类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21790330/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com