所謂測試驅動開發,英文全稱Test-Driven Development,簡稱TDD,是一種不同於傳統軟件開發流程的新型的開發方法。就是在明確要開發某個功能后,首先思考如何對這個功能進行測試,並完成測試代碼的編寫,然后編寫相關的代碼滿足這些測試用例。然后循環進行添加其他功能,直到完成全部功能的開發。
Google Mock的設計靈感來源於jMock和EasyMock,它的作用是幫你快速地做出一個接口的仿制品。如果你的設計依賴其它的類,而這些類還沒有完成或非常昂貴(如數據庫);如果你要測試你的模塊與其它模塊是否能正確結合,並想了解其交互過程;那么Google Mock就能幫助你。
PS: The official Google Mock site is https://code.google.com/p/googlemock/. The version used for building the examples is Google Mock 1.7.0.
一、 環境配置
gmock1.7.0中使用了C++11新標准,所以我們的編譯器需要支持C++11才行,在Linux系統中,即需要安裝GCC4.7/G++4.7,我的測試環境是Ubuntu12.04,默認安裝的是GCC4.6/G++4.6,所以需要在安裝編譯gmock之前首先安裝GCC4.7/G++4.7,這里也順便把安裝的過程加上,有需要的猿們可以參考:
1 sudo add-apt-repository ppa:ubuntu-toolchain-r/test 2 sudo apt-get update 3 sudo apt-get install gcc-4.7 g++-4.7
安裝成功后我們如果要使用gcc-4.7&g++-4.7來編譯的話,我們就得把gcc改為gcc-4.7,g++同理,改為g++-4.7來進行編譯.如果你想直接使用gcc-4.7而不改變編譯時gcc改為gcc-4.7的話,我們就可以更改一下gcc的軟鏈接:
1 sudo rm /usr/bin/gcc 2 sudo ln -s /usr/bin/gcc-4.7 /usr/bin/gcc 3 sudo rm /usr/bin/g++ 4 sudo ln -s /usr/bin/g++-4.7 /usr/bin/g++
PS: 在平時使用的時候如果使用C++0X標准,記得加-std=c++11。
二、gmock安裝
下載好gmock之后,解壓,然后切換到gmock源碼所在目錄,使用如下命令安裝:
1 mkdir mybuild 2 cd mybuild 3 cmake .. 4 make
同時,你還需要編譯google test,其包含在gmock源碼下的gtest文件夾,切換到gtest文件夾,然后用相同的方式安裝即可。
三、實例
Soundex.h文件:
1 #ifndef Soundex_h 2 #define Soundex_h 3 #include <string> 4 5 class Soundex 6 { 7 public: 8 std::string encode(const std::string& word) const { 9 return zeroPad(word); 10 } 11 12 private: 13 std::string zeroPad(const std::string& word) const { 14 return word + "000"; 15 } 16 }; 17 18 #endif
SoundexTest.cpp文件:
1 #include "gmock/gmock.h" 2 #include "Soundex.h" 3 4 using namespace testing; 5 6 class SoundexEncoding: public Test { 7 public: 8 Soundex soundex; 9 }; 10 11 TEST_F(SoundexEncoding, RetainsSoleLetterOfOneLetterWord) { 12 ASSERT_THAT(soundex.encode("A"), Eq("A000")); 13 } 14 15 TEST_F(SoundexEncoding, PadsWithZerosToEnsureThreeDigits) { 16 ASSERT_THAT(soundex.encode("I"), Eq("I000")); 17 }
main.cpp文件:
1 #include "gmock/gmock.h" 2 3 int main(int argc, char** argv) { 4 testing::InitGoogleMock(&argc, argv); 5 return RUN_ALL_TESTS(); 6 }
CMakeLists.txt文件:
1 project(chapterFirstExample) 2 cmake_minimum_required(VERSION 2.6) 3 4 include_directories($ENV{GMOCK_HOME}/include $ENV{GMOCK_HOME}/gtest/include) 5 link_directories($ENV{GMOCK_HOME}/mybuild $ENV{GMOCK_HOME}/gtest/mybuild) 6 add_definitions(-std=c++0x) 7 set(CMAKE_CXX_FLAGS "${CMAXE_CXX_FLAGS} -Wall") 8 9 set(sources 10 main.cpp 11 SoundexTest.cpp) 12 add_executable(test ${sources}) 13 target_link_libraries(test pthread) 14 target_link_libraries(test gmock) 15 target_link_libraries(test gtest)
好了,編譯執行吧,執行結果如下:
好了gmock的使用就介紹到這里,需要深入研究的童鞋可以參考官方文檔。這里最重要的不是學會使用gmock,而是要在學會使用gmock之后養成TDD開發的好習慣.
Test-driving vs Testing: Using a testing technique, you would seek to exhaustively analyze the specification in question (and possibly the code) and devise tests that exhaustively cover the behavior. TDD is instead a technique for driving the design of the code. In TDD, you write tests to describe the next behavior needed.