使用 googletest & googlemock 做單元測試

  1. svn checkout http://googlemock.googlecode.com/svn/trunk/ (已包含 googletest)
  2. 將來可以直接用 svn update
  3. 為了方便將這兩個 Framework 加入測試專案,將輸出目錄由 SolutionName 改為 SolutionDir,避免附加到不同的 Solution 時造成名稱不一致 (後來發現一般  Project 都是用 SolutionDir,唯獨這兩個 Project 使用 SolutionName,怪哉!)
    • 開啟 googlemock\gtest\msvc\gtest-md.sln,點選 gtest-md 的 Properties,選擇 All Configurations,將 General | Output Directory 由 $(SolutionName)/$(ConfigurationName) 改為  $(SolutionDir)/$(ConfigurationName)
    • 開啟 googlemock\msvc\2005\gmock.sln
      • 點選 gmock 的 Properties,選擇 All Configurations,將 General | Output Directory 由 $(SolutionName)/$(ConfigurationName) 改為  $(SolutionDir)/$(ConfigurationName)
      • 選擇 Debug Configuration,將 C/C++ | Code Generation | Runtime Library 由 Multi-threaded Debug (/MTd) 改為 Multi-threaded Debug DLL (/MDd)
      • 選擇 Release Configuration,將 C/C++ | Code Generation | Runtime Library 由 Multi-threaded (/MT) 改為 Multi-threaded DLL (/MD)
以上動作設定一次即可

PS :

如果 Project Runtime Library  使用 Multi-threaded (Debug) DLL,則 MFC 必須使用 Shared DLL;反之若使用 Multi-threaded (Debug),則 MFC 必須搭配 Static Library。而且整個 Solution 相關的 Project 必須一致,否則會產生 Error LNK2005 的錯誤。

通常 Debug 版本為了做單元測試,所以必須使用 Shared DLL 版本;而 Release 版本為了防止程式執行安裝時 MFC 版本錯誤,所以採用 Static Library。

  1. 待測專案使用 Win32 Static library + MFC
  2. 測試專案使用 Win32 Console application + MFC
  3. 將 googletest 與 googlemock 視需求加入現有的方案中
  4. 開啟主專案的 Property
    • 點選 Framework and References | Add New Reference,選擇 Static library 專案
    • 點選 C/C++ | General,選擇 All Configurations,在 Additional Include Directories 加入 Static library 的路徑
  5. 開啟測試專案的 Property
    • 點選 Framework and References | Add New Reference,選擇 Static library、googletest & googlemock
    • 點選 C/C++ | General,選擇 All Configurations,在 Additional Include Directories 加入 Static library、googletest & googlemock 的路徑
  6. 參考 googletest & googlemock 說明撰寫測試案例

留言

這個網誌中的熱門文章

Linux 批次檔的寫法

【分享】如何顯示 Debug Message

[分享] Visual Studio 遠端偵錯