2011-03-18 34 views
0

我們將cppunit unittests構建爲一個dll並將其加載到TestPlugInRunnerd.exe中以顯示我們的結果。我們寫自己的模擬,但我想開始使用模擬框架,如gmock。TestPlugInRunnerd.exe + gmock

我下載了gmock,並且沒有太多的問題。我用gmock寫了一個模擬器,它編譯得很好。但後來我讀了gmock常見問題解答中的以下內容:

 
If you want to use something other than Google Test (e.g. CppUnit or CxxTest) as your testing framework, just change the main() function in the previous section to: 

int main(int argc, char** argv) { 
    // The following line causes Google Mock to throw an exception on failure, 
    // which will be interpreted by your testing framework as a test failure. 
    ::testing::GTEST_FLAG(throw_on_failure) = true; 
    ::testing::InitGoogleMock(&argc, argv); 
    ... whatever your testing framework requires ... 
} 

This approach has a catch: it makes Google Mock throw an exception from a mock object's destructor sometimes. With some compilers, this sometimes causes the test program to crash. You'll still be able to notice that the test has failed, but it's not a graceful failure. 

我明顯沒有一個main。我需要做些什麼才能讓gmock與我的dll一起工作?我應該考慮替代gmock嗎?

感謝,

巴里

回答

1

首先,這是不可能的,你沒有一個主力。否則,你將如何執行你的單元測試?

其次,你可以創建一個類的靜態變量,其中調用這兩個函數,就像這樣:

struct GmockInitializer 
{ 
    GmockInitializer() 
    { 
    ::testing::GTEST_FLAG(throw_on_failure) = true; 
    ::testing::InitGoogleMock(0,0); // << not sure about this. might not work 
    } 
}; 
GmockInitializer gmockInitializer; 
+0

謝謝,但我想通了,你可以使用環境變量GTEST_THROW_ON_FAILURE = 1 – Baz 2011-03-18 09:29:53

+0

@巴茲好吧,但你怎麼初始化它? – 2011-03-18 09:36:03

+0

@BЈовић你不需要DLL中的主體,只需調用gmock初始化並運行測試的外部函數。然後可以通過加載DLL並調用外部函數從TestPlugInRunnerd.EXE調用該函數。我猜這是他如何做到的。 – 2012-11-13 22:34:14