简体   繁体   English

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

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

I am trying to use typed test concept available in google test.我正在尝试使用谷歌测试中可用的类型化测试概念。 The description of this concept matches what I intend to do, but I cannot figure it out completely.这个概念的描述符合我打算做的,但我无法完全弄清楚。 I want to test structs which implements an interface, since they are totally different once, they need to be initialized with different values/instances.我想测试实现接口的结构,因为它们一次完全不同,它们需要用不同的值/实例进行初始化。

Simply my code is as follows简单地我的代码如下

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

And I want to test s1 and s2 with different instances/values.我想用不同的实例/值测试 s1 和 s2。 The test should look like:测试应如下所示:

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

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

Can someone please tell me if this is possible to do and how?有人可以告诉我这是否可行以及如何做? Many thanks in advance提前谢谢了

I finally figured it out. 我终于弄明白了。 for the example that I had above. 我上面的例子。 It will be like: 它将像:

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

The original sample can't be compiled, I post my successful change for reference.原始示例无法编译,我发布我的成功更改以供参考。 If you need the full source code, please refer to the link( https://github.com/dougpuob/googletest-sample/blob/master/source/test-derived-func-by-interface/main.cpp ).如果您需要完整的源代码,请参阅链接( 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;
}

It's more convenient to use value-parameterized tests for testing different implementations of an interface. 使用值参数化测试来测试接口的不同实现更为方便。 Google Test's sample7 shows how to do that. Google Test的sample7展示了如何做到这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM