谷歌测试中测试夹具的多重定义

时间:2012-02-28 19:16:08

标签: googletest

我在.hpp文件中有一组通用单元测试,其中必须包含多个测试文件。

但是它获得了同一文件的多个副本以及关于测试装置的多个定义的通用.hpp文件投诉。

需要有关如何处理此问题的帮助。

1 个答案:

答案 0 :(得分:1)

您应该能够使用.hpp和.cpp文件以通常的方式将gtest类声明与定义分开。

因此,不是在标题中定义测试函数和fixture,而是将它们移动到#include标题的源文件中。如果是,例如你有test.hpp

#include "gtest/gtest.h"

class MyTest : public ::testing::Test {
 protected:
  void TestFunction(int i) {
    ASSERT_GT(10, i);
  }
};

TEST_F(MyTest, first_test) {
  ASSERT_NE(1, 2);
  TestFunction(9);
}

test.hpp更改为:

#include "gtest/gtest.h"

class MyTest : public ::testing::Test {
 protected:
  void TestFunction(int i);
};

并添加test.cpp

#include "test.hpp"

void MyTest::TestFunction(int i) {
  ASSERT_GT(10, i);
}

TEST_F(MyTest, first_test) {
  ASSERT_NE(1, 2);
  TestFunction(9);
}

如果您在多个地方包含相同的测试标题,您真的在寻找类型化测试或类型参数化测试吗?有关详细信息,请参阅http://code.google.com/p/googletest/wiki/V1_6_AdvancedGuide#Typed_Tests

相关问题