2

假设我写了这样的代码。

template <typename T>
  class FooTest : public testing::Test
  {
     //class body
  };
  typedef ::testing::Types<int, float/*, and few more*/> TestTypes;
  TYPED_TEST_CASE(FooTest, TestTypes);


  TYPED_TEST(FooTest, test1)
  {
     //...
  }
  TYPED_TEST(FooTest, test2)
  {
     //...;
  }

是否有可能仅针对TestTypes中指定的一种数据类型运行(例如第二次测试)并避免任何代码重复?

4

1 回答 1

2

您应该能够通过使用继承创建第二个测试类来做到这一点。

这类似于两个常见问题条目的方法:

我可以从另一个派生测试夹具吗?

我有几个测试用例共享相同的逻辑......

在下面的代码大纲中,我将类型分成两个不相交的集合。

  template <typename T>
  class FooTest : public testing::Test
  {
     //class body
  };

  // TestTypes only contains some of the types as before
  // in this example, floating point types are tested only with FooTest.
  typedef ::testing::Types<float, double, /*, and few more*/> TestTypes;

  TYPED_TEST_CASE(FooTest, TestTypes);

  TYPED_TEST(FooTest, test1)
  {
     //...
  }

  // Optional, but could allow you to reuse test constructor 
  // and SetUp/TearDown functions
  template <typename T>
  class ExtendedFooTest : public FooTest<T>
  {}

  // And integral types are given extended tests
  typedef ::testing::Types<int, unsigned int, and few more*/> ExtendedFooTypes;

  TYPED_TEST_CASE(FooTest, ExtendedFooTypes);         // repeat the tests above
  TYPED_TEST_CASE(ExtendedFooTest, ExtendedFooTypes); // Plus add new tests.

  TYPED_TEST(ExtendedFooTest, test2)
  {
     //...;
  }
于 2013-11-03T19:12:19.530 回答