2012-02-28 41 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