如何使用谷歌测试测试实现界面的不同类/结构?

时间:2016-03-20 14:48:20

标签: c++ googletest

我正在尝试使用谷歌测试中提供的键入测试概念。这个概念的描述符合我打算做的,但我无法完全理解。我想测试实现接口的结构,因为它们完全不同,需要使用不同的值/实例进行初始化。

只需我的代码如下

struct Serializable
{
    virtual sObj serialize() = 0;
    virtual void unserialize(sObj) = 0;
};

struct s1 : serializable
{
  int attrI1;
  int attrI2;
  sObj serialize()
  {
    //serialize an instance of this struct
  }
  void unserialize(sObj)
  {
    //unserialize data to instance of this struct
  }
}

struct s2 : serializable
{
  char attrC;
  void serialize()
  {
    //serialize an instance of this struct
  }
  sObj unserialize()
  {
    //unserialize data to instance of this struct
  }
}

我想用不同的实例/值测试s1和s2。测试应该如下:

template <typename T>
int testSerialzable(T& t)
{
  sObj obj = t.pack();

  T temp; 
  TEST_EQ(temp.unpack(obj), t); 
}

有人可以告诉我这是否可行以及如何做? 非常感谢提前

3 个答案:

答案 0 :(得分:1)

我终于明白了。我上面的例子。它会像:

template<class T>
struct TestSerializable : public ::testing::Test
{
    static T serializedType;
};

TYPED_TEST_CASE_P(TestSerializable);

TYPED_TEST_P(TestSerializable, serializationTest)
{
  sObj obj = t.serialize();

  TypeParam temp; 
  ASSERT_EQ(temp.unserialize(obj), t); 
}

REGISTER_TYPED_TEST_CASE_P(TestSerializable, serializationTest);


typedef ::testing::Types<s1, s2> MyTypes;
INSTANTIATE_TYPED_TEST_CASE_P(MySerialiInstantiation, TestSerializable, MyTypes);


template<> s1 TestSerializable<s1>::serializedType(/*instance of s1 with proper values*/s1());
template<> s2 TestSerializable<s2>::serializedType(/*instance of s1 with proper values*/s2());

答案 1 :(得分:0)

原始示例无法编译,我将成功更改发布以供参考。如果您需要完整的源代码,请参阅链接(https://github.com/dougpuob/googletest-sample/blob/master/source/test-derived-func-by-interface/main.cpp)。

#include "BinarySearchLoop.h"
#include "BinarySearchRecursive.h"
#include "gtest/gtest.h"

template <class T>
struct TestBinarySearch : public ::testing::Test {
  static T Instance;
};

TYPED_TEST_CASE_P(TestBinarySearch);

TYPED_TEST_P(TestBinarySearch, PositiveInteger) {
  std::vector<int> SortedArray = {1, 2, 3, 4, 5, 6, 7, 8, 9};
  ASSERT_EQ(4, Instance.search(SortedArray, 5));
  ASSERT_EQ(5, Instance.search(SortedArray, 6));
  ASSERT_EQ(0, Instance.search(SortedArray, 1));
  ASSERT_EQ(8, Instance.search(SortedArray, 9));
}

TYPED_TEST_P(TestBinarySearch, NegativeInteger) {
  std::vector<int> SortedArray = {-8, -7, -6, -5, -4, -3, -2, -1};
  EXPECT_EQ(+6, Instance.search(SortedArray, -2));
  EXPECT_EQ(-1, Instance.search(SortedArray, +0));
  EXPECT_EQ(-1, Instance.search(SortedArray, -9));
}

TYPED_TEST_P(TestBinarySearch, Integer) {
  std::vector<int> SortedArray = {-1, 0, 3, 5, 9, 12};
  EXPECT_EQ(+4, Instance.search(SortedArray, 9));
  EXPECT_EQ(-1, Instance.search(SortedArray, 2));
}

TYPED_TEST_P(TestBinarySearch, SingleElement) {
  std::vector<int> SortedArray = {5};
  EXPECT_EQ(+0, Instance.search(SortedArray, 5));
  EXPECT_EQ(-1, Instance.search(SortedArray, 1));
  EXPECT_EQ(-1, Instance.search(SortedArray, 6));
}

TYPED_TEST_P(TestBinarySearch, TwoElementsOnly) {
  std::vector<int> SortedArray = {1, 5};
  EXPECT_EQ(+1, Instance.search(SortedArray, 5));
  EXPECT_EQ(+0, Instance.search(SortedArray, 1));
  EXPECT_EQ(-1, Instance.search(SortedArray, 2));  // Not in the array.
  EXPECT_EQ(-1, Instance.search(SortedArray, 7));  // Not in the array.
}

REGISTER_TYPED_TEST_CASE_P(TestBinarySearch,
                           PositiveInteger,
                           NegativeInteger,
                           Integer,
                           SingleElement,
                           TwoElementsOnly);

typedef ::testing::Types<BinarySearchLoop, BinarySearchRecursive> MyTypes;
INSTANTIATE_TYPED_TEST_CASE_P(MyBinarySearchInstantiation,
                              TestBinarySearch,
                              MyTypes);

template <>
BinarySearchLoop TestBinarySearch<BinarySearchLoop>::Instance;

template <>
BinarySearchRecursive TestBinarySearch<BinarySearchRecursive>::Instance;

int main(int argc, char** argv) {
  ::testing::InitGoogleTest(&argc, argv);
  int Ret = RUN_ALL_TESTS();
  return Ret;
}

答案 2 :(得分:-2)

使用value-parameterized tests测试界面的不同实现更方便。 Google Test的sample7展示了如何做到这一点。