GTest:TYPE_TESTING全局功能时需要固定装置吗?

时间:2018-09-06 13:53:55

标签: unit-testing googletest

我想使用table.addListener(SWT.Selection, new Listener() { private int lastSelected = -1; @Override public void handleEvent(final Event event) { final int selectedIndex = table.getSelectionIndex(); if (selectedIndex < 0) { lastSelected = -1; return; } if (selectedIndex == lastSelected) { table.deselect(selectedIndex); lastSelected = -1; } else { lastSelected = selectedIndex; } } }); 对一些全局模板化函数进行单元测试。以下代码有效,我只是想知道是否有办法摆脱测试夹具,因为它似乎不是必需的。

TYPED_TEST

1 个答案:

答案 0 :(得分:1)

  

...因为似乎不需要

这是必需的。看到单元测试源的这种独立的简化 (它的名字ReturnsMinimumValue毫无意义,但这没关系):

gtester.cpp

#include <gtest/gtest.h>

template<typename T>
struct foo {
    static T bar() {
        return 0;
    }
};

template <class T>
class MinTest : public testing::Test {};

typedef ::testing::Types<int, float> Implementations;

TYPED_TEST_CASE(MinTest, Implementations);

TYPED_TEST(MinTest, ReturnsMinimumValue)
{
    EXPECT_EQ(foo<TypeParam>::bar(),0);
}
int main(int argc, char **argv) {
    ::testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}

编译,链接并运行:

$ g++ -Wall -Wextra -o gtester gtester.cpp -lgtest -pthread && ./gtester
[==========] Running 2 tests from 2 test cases.
[----------] Global test environment set-up.
[----------] 1 test from MinTest/0, where TypeParam = int
[ RUN      ] MinTest/0.ReturnsMinimumValue
[       OK ] MinTest/0.ReturnsMinimumValue (0 ms)
[----------] 1 test from MinTest/0 (0 ms total)

[----------] 1 test from MinTest/1, where TypeParam = float
[ RUN      ] MinTest/1.ReturnsMinimumValue
[       OK ] MinTest/1.ReturnsMinimumValue (0 ms)
[----------] 1 test from MinTest/1 (0 ms total)

[----------] Global test environment tear-down
[==========] 2 tests from 2 test cases ran. (0 ms total)
[  PASSED  ] 2 tests.

现在让我们预处理源文件,并预处理经过预处理的输出(因为它不漂亮):

$ g++ -E -P gtester.cpp | clang-format > gtester.ii

查看输出的最后50多行:

gtester.ii

...
...
int RUN_ALL_TESTS() __attribute__((warn_unused_result));
inline int RUN_ALL_TESTS() { return ::testing::UnitTest::GetInstance()->Run(); }
template <typename T> struct foo {
  static T bar() { return 0; }
};
template <class T> class MinTest : public testing::Test {};
typedef ::testing::Types<int, float> Implementations;
typedef ::testing::internal::TypeList<Implementations>::type
    gtest_type_params_MinTest_;
template <typename gtest_TypeParam_>
class MinTest_ReturnsMinimumValue_Test : public MinTest<gtest_TypeParam_> {
private:
  typedef MinTest<gtest_TypeParam_> TestFixture;
  typedef gtest_TypeParam_ TypeParam;
  virtual void TestBody();
};
bool gtest_MinTest_ReturnsMinimumValue_registered_ __attribute__((unused)) =
    ::testing::internal::TypeParameterizedTest<
        MinTest,
        ::testing::internal::TemplateSel<MinTest_ReturnsMinimumValue_Test>,
        gtest_type_params_MinTest_>::Register("",
                                              ::testing::internal::CodeLocation(
                                                  "gtester.cpp", 17),
                                              "MinTest", "ReturnsMinimumValue",
                                              0);
template <typename gtest_TypeParam_>
void MinTest_ReturnsMinimumValue_Test<gtest_TypeParam_>::TestBody() {
  switch (0)
  case 0:
  default:
    if (const ::testing::AssertionResult gtest_ar =
            (::testing::internal::EqHelper<(
                 sizeof(::testing::internal::IsNullLiteralHelper(
                     foo<TypeParam>::bar())) ==
                 1)>::Compare("foo<TypeParam>::bar()", "0",
                              foo<TypeParam>::bar(), 0)))
      ;
    else
      ::testing::internal::AssertHelper(
          ::testing::TestPartResult::kNonFatalFailure, "gtester.cpp", 19,
          gtest_ar.failure_message()) = ::testing::Message();
}
int main(int argc, char **argv) {
  ::testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}

我们的

TYPED_TEST(MinTest, ReturnsMinimumValue)
{
    EXPECT_EQ(foo<TypeParam>::bar(),0);
}

扩展为以下内容的定义:

template <typename gtest_TypeParam_>
class MinTest_ReturnsMinimumValue_Test;

其源自模板夹具:

template <class T> class MinTest;

派生自:

testing::Test;
MinTest_ReturnsMinimumValue_Test继承的

virtual void TestBody();

并将其覆盖为:

void MinTest_ReturnsMinimumValue_Test<gtest_TypeParam_>::TestBody() {
  switch (0)
  case 0:
  default:
    if (const ::testing::AssertionResult gtest_ar =
            (::testing::internal::EqHelper<(
                 sizeof(::testing::internal::IsNullLiteralHelper(
                     foo<TypeParam>::bar())) ==
                 1)>::Compare("foo<TypeParam>::bar()", "0",
                              foo<TypeParam>::bar(), 0)))
      ;
    else
      ::testing::internal::AssertHelper(
          ::testing::TestPartResult::kNonFatalFailure, "gtester.cpp", 19,
          gtest_ar.failure_message()) = ::testing::Message();
}

实现以下内容的扩展:

EXPECT_EQ(foo<TypeParam>::bar(),0);

不同的测试框架可能会完成类型参数化的测试用例 有所不同,但这就是googletest的工作方式。

多态类的层次结构:

template <typename gtest_TypeParam_>
class MinTest_ReturnsMinimumValue_Test
    <-
template <class T> class MinTest
    <-
testing::Test
由于任何假设,未生成

被测方法调用 是多态的。 foo<TypeParam>::bar()当然不是。多态 之所以会生成类层次结构,是因为这是googletest的基本设计。