0

I have a set of generic unit tests in a .hpp file that more than one test file must include.

But it gets multiple copies of the same file and the generic .hpp file complaints about multiple definition of the Test fixtures.

Need help on how to approach this.

user1065969
  • 589
  • 4
  • 10
  • 19

1 Answers1

2

You should be able to separate the gtest class declarations from the definitions in the usual way using .hpp and .cpp files.

So rather than defining the test functions and fixtures in the header, move these to a source file which #includes the header. So if e.g. you have test.hpp as:

#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);
}

change test.hpp to:

#include "gtest/gtest.h"

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

and add 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);
}

If you're including the same test header in multiple places are you really looking for typed tests or type-parameterised tests? See http://code.google.com/p/googletest/wiki/V1_6_AdvancedGuide#Typed_Tests for further details.

Fraser
  • 74,704
  • 20
  • 238
  • 215