2012-02-28 79 views
0

我在.hpp文件中有一组通用的单元测试,多个测试文件必须包含这些测试文件。谷歌测试中测试装置的多重定义

但它获得了多个相同文件的副本和通用.hpp文件关于Test fixture的多重定义的抱怨。

需要关于如何解决这个问题的帮助。

回答

1

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

因此,不要在头文件中定义测试函数和夹具,而要将这些文件移动到头文件的源文件。所以如果例如你有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

+0

谢谢你弗雷泽! – user1065969 2012-02-29 16:08:50