如何使用 google 测试来测试实现接口的不同 classes/structs?

How to test different classes/structs implementing an interface using google test?

我正在尝试使用 google 测试中可用的类型化测试概念。这个概念的描述符合我打算做的事情,但我无法完全理解。我想测试实现接口的结构,因为它们曾经完全不同,所以需要用不同的 values/instances.

进行初始化

简单我的代码如下

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
  }
}

我想用不同的 instances/values 测试 s1 和 s2。测试应如下所示:

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

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

有人可以告诉我这是否可行以及如何实现吗? 非常感谢

使用起来更方便value-parameterized tests for testing different implementations of an interface. Google Test's sample7展示了如何做到这一点。

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

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());

原样编译不了,我post我改成功供参考。如需完整源码,请参考link(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;
}