仅对指定类型之一运行typed_test

时间:2013-09-04 11:45:27

标签: c++ googletest

我们假设我写了这样的代码。

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 中指定的仅一种数据类型运行(例如第二次测试)并避免任何代码重复?

1 个答案:

答案 0 :(得分:2)

您应该可以通过使用inheiritance创建第二个测试类来实现此目的。

这类似于两个faq条目的方法:

Can I derive a test fixture from another?

I have several test cases which share the same logic...

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

  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)
  {
     //...;
  }